【发布时间】:2015-04-03 18:41:50
【问题描述】:
string conStr = null;
SqlCommand cmd;
SqlConnection cnn;
string sql = null;
conStr = "Data Source=DELL-PC\\SQLEXPRESS;Initial Catalog=DBMSI;Integrated Security=True";
sql = "insert into CEC_Employee values('"+empid + "','" + name + "','" + fname + "','" + mname + "','" + lname + "','" + address + "','" + postcode + "','" + job + "','" + sdate + "','" + whours + "','" + sph + "','" + spa + "','" + location + "','" + working + "','" + gender + "','" + dob + "','" + pn + "','" + exp + "','" + vtype + "','" + vexp + "','" + qualification + "','" + email + "','" + number + "','" + nin + "','" + sort + "','" + acc + "','" + bank + "','" + nname + "','" + rel + "','" + addkin + "','" + cnokin + "','" + emailkin + "')";
cnn = new SqlConnection(conStr);
try
{
cnn.Open();
cnn = new SqlConnection(conStr);
cmd = new SqlCommand(sql, cnn);
cmd.ExecuteNonQuery();
cmd.Dispose();
cnn.Open();
MessageBox.Show("Employee Details registered Succesffuly");
// Keeps on moving to the Exception part of the code. Doesn't execute the try portion of the program.
}
catch (Exception ex)
{
MessageBox.Show("Error Occoured - Employee Details were not recorded");
}
在网上找到了代码。请帮助使其工作。谢谢!
【问题讨论】:
-
请花点时间仔细查看:How to Ask
-
尝试查看
ex.Message并查看内容。它将为您提供有关错误的更多信息。 -
“不起作用”是一个完全无用的问题陈述。请尽可能详细地告诉我们您正在尝试做什么以及它到底是如何不起作用的。
-
删除可能引发异常的第二个
cnn.Open(); -
警告您的代码极易受到 sql 注入攻击!
标签: c# sql-server-2008 ado.net executenonquery