我的列比我在这里给你的要多得多,但我只是想发布主键列 TrackingID (nchar[10]) 作为测试 - 如果我能让它工作,我可以得到其余的人去工作。我有一个 Windows 窗体应用程序,它可以进行类似的查询并完美运行,所以看到它是 C#,我很难理解出了什么问题。是连接字符串还是sql查询?表名是完整的。总共有 23 列,但唯一的主键是 Tracking ID,不能为空。在所有情况下,这些查询都是 OnClick 按钮事件。如您所见,我尝试了几条路线。我希望将其设置为参数化以防止 SQL 注入。您将看到的第一段代码来自 C# 中的 Windows 窗体应用程序,该应用程序(虽然易受攻击)始终如一地工作。下面,您将看到 asp.net 代码。
private void licensesubmitbutton_Click(object sender, EventArgs e)
{
try
{
System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection("Data Source=data99;Initial Catalog=LicenseInventoryMgr;Integrated Security=True;Connect Timeout=0;Trusted_Connection=Yes");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT LIMTable (Software,Host,AssetTag,ActivationDate,LicenseNumber) VALUES ('" + softwarecombobox.Text + "','" + hostnamebox.Text + "','" + assettagcombobox.Text + "','" + dateactivateddatetimepicker.Value.ToString("yyyy-MM-dd") + "','" + licensekeytextbox.Text + "')";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
MessageBox.Show("Successfully Submitted!");
}
catch (System.Exception )
{
MessageBox.Show("Submission unsuccessful. Try saving and refreshing first!");
}
}
这是我尝试过的 ASP.NET 代码。
protected void submit_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(@"Data Source=data99;Initial Catalog=LFM_Archive;Integrated Security=True");
string sql = "INSERT INTO Full (TrackingID) values (@TrackingID)";
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@TrackingID", SqlDbType.NChar);
cmd.Parameters["@TrackingID"].Value = 6789;
cmd.ExecuteNonQuery();
conn.Close();
MailMessage m1 = new MailMessage("y@x.net", "x@x.net");
m1.Subject = "SQL Query Success";
m1.Body = "SQL Query Success :)";
m1.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "mailserver99";
smtp.EnableSsl = false;
//System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
//NetworkCred.UserName = "username";
//NetworkCred.Password = "password";
smtp.UseDefaultCredentials = true;
//smtp.Credentials = NetworkCred;
smtp.Port = 25;
smtp.Send(m1);
}
catch
{
MailMessage m2 = new MailMessage("y@x.net", "x@x.net");
m2.Subject = "SQL Query Failed";
m2.Body = "SQL Query Failed :(";
m2.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "mailserver99";
smtp.EnableSsl = false;
smtp.UseDefaultCredentials = true;
smtp.Port = 25;
smtp.Send(m2);
}
}
最终结果在从中提取和/或发送电子邮件的表单窗口中显示查询成功或失败。当我不再需要对其进行测试时,电子邮件将是表单已发布到存档的简单通知。asp.net应用程序中的注释代码仅表示我尝试过,当它不起作用时,我对其进行注释并尝试了一种新方法。
有人建议我尝试输入一条异常消息以更好地查看 - 这对一些人有所帮助。submitsuccess.Text 是消息出现的地方。结果如下:“关键字'Full'附近的语法不正确。”
这听起来像是查询。“Full”是数据库中的表名。
回答 这是有效的:
SqlConnection conn = new SqlConnection(@"Data Source=data99;Initial Catalog=LFM_Archive;Integrated Security=True");
string sql = "INSERT INTO [Full] (TrackingID) values (@TrackingID)";
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@TrackingID", SqlDbType.NChar, 10);
cmd.Parameters["@TrackingID"].Value = empfirst.Text;
cmd.ExecuteNonQuery();
conn.Close();