【发布时间】:2018-05-18 09:02:56
【问题描述】:
嘿,这是我的代码,这是我得到的错误:
抛出异常:“System.Data.SqlClient.SqlException”在 System.Data.dll ("')' 附近的语法不正确。")
void addMember()
{
try
{
//con.Open();
String EmpID = txtEmpID.Text.ToString().Trim();
String EmployeeName = txtEmpID.Text.ToString().Trim();
String UserName = txtUsername.Text.ToString().Trim();
String UserType = cmbUserType.GetItemText(cmbUserType.SelectedItem);
String PassWord = txtPassword.Text.ToString().Trim();
SqlCommand addUser = new SqlCommand("insert into userDetails(@empID ,@employeeName ,@userName , @userType, @password)", con);
addUser.Parameters.Add(new SqlParameter("@empID", EmpID));
addUser.Parameters.Add(new SqlParameter("@employeeName", EmployeeName));
addUser.Parameters.Add(new SqlParameter("@userName", UserName));
addUser.Parameters.Add(new SqlParameter("@userType", UserType));
addUser.Parameters.Add(new SqlParameter("@password", PassWord));
int x = addUser.ExecuteNonQuery();
if (x > 0)
{
MessageBox.Show("Data Inserted", "User Form", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//con.Close();
}
catch (Exception ex)
{
MessageBox.Show("" + ex);
}
【问题讨论】:
-
不要在未指定列名的情况下将插入写入查询。如果出于任何原因向该表添加新列,您的代码将中断。 (你会从一开始就避免这个错误)
-
为什么有人在这里贴代码,却又慌张又拒绝学习使用调试器..?
Google is a great free tool for learning SQL Syntax -
调试器在这里并不能真正帮助您……这是一个 sql 语法问题。但是,您是否有理由需要使用原始 SQL 命令和实体框架?
标签: c# sql sql-server exception ado.net