' System.Data.SqlClient.SqlException'在System.Data.dll中("'附近的语法不正确)'。")?

时间:2017-12-04 20:04:45

标签: c# sql sql-server exception ado.net

嘿,这是我的代码,这是我得到的错误:

  

抛出异常:' 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);
    }

1 个答案:

答案 0 :(得分:7)

您错过了Values关键字:

SqlCommand addUser = new SqlCommand("insert into userDetails Values(@empID ," +
                                                              ^^^^
                                    "@employeeName ,@userName , @userType, @password)", con);