【问题标题】:SQL syntax error near 'fieldname' [closed]'fieldname'附近的SQL语法错误[关闭]
【发布时间】:2018-06-21 13:08:43
【问题描述】:

我是 C# 的新手,我有一个数据库,我需要用 windows 窗体填充,将数据插入表的按钮具有以下代码:

private void btnAddEmployee_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("server=.\\server1; database = PMS; Integrated security=true;");
            SqlDataAdapter da = new SqlDataAdapter("INSERT INTO tblEmployees cid, empID, empFirstName, empMidName, "
                +"empLastName, empAge, empTitle, empAddress, empRank, empSalary, empEmail, empPhone, "
                +"empMobile, Notes, userName, usrPassword, usrAccessLevel, empActive, empMarked, empType "
                +"VALUES ('" + this.txtID + "', '" + this.txtEmpID + "', '" + this.txtFirstName + "','" + this.txtMidName + "'," +
                " '" + this.txtLastName + "', '" + this.txtEmpAge + "', '" + this.txtJobTitle + "', '" + this.txtAddress + "', " +
                " '" + this.cmbRank + "', '" + this.txtSalary + "', '" + this.txtEmail + "', '" + this.txtPhone + "', " + 
                " '" + this.txtMobile + "', '" + this.txtNote + "', '" + this.txtUserName + "', '" + this.txtPassword + "', " + 
                " '" + this.cmbAcsLevel + "', '" + this.txtActive + "', '" + this.txtMarked + "', '" + this.txtType + "')", cn);

        if (cn.State != ConnectionState.Open)
        {
            cn.Open();
        }
        object o = da.SelectCommand.ExecuteNonQuery();
        cn.Close();
    }

但是点击按钮后我得到以下错误:

System.Data.SqlClient.SqlException: 'cid' 附近的语法不正确。'

【问题讨论】:

  • 字段名称周围缺少括号。应该是INSERT INTO tblEmployees (cid,..... empType )
  • 另外,阅读 SQL 注入以及如何使用参数化查询来防止它。您的代码非常不安全。
  • 即使使用表单应用程序,您也应该练习安全并使用 parameters 将 SQL codedata 分开,而不是通过字符串操作将它们组合在一起
  • 另外考虑不要将数字数据存储为字符串(例如薪水)。还可以考虑存储出生日期,而不是年龄(因为年龄不断变化)。

标签: c# sql visual-studio


【解决方案1】:

首先让它变得简单,使用Parameterized query来防止sql injection

     using(SqlConnection connection = new SqlConnection("server=.\\server1; database = PMS; Integrated security=true;"))
      {
     String query = "INSERT INTO tblEmployees (cid, empID, empFirstName, empMidName,empLastName, empAge, empTitle, empAddress, empRank, empSalary, empEmail, empPhone,empMobile, Notes, userName, usrPassword, usrAccessLevel, empActive, empMarked, empType) VALUES (@cid, @empID, @empFirstName, @empMidName,@empLastName, @empAge, @empTitle, @empAddress, @empRank, @empSalary, @empEmail, @empPhone,@empMobile, @Notes, @userName, @usrPassword, @usrAccessLevel, @empActive, @empMarked, @empType)";

using(SqlCommand command = new SqlCommand(query, connection))
{
    command.Parameters.AddWithValue("@cid", cid);
    command.Parameters.AddWithValue("@empID", empID);
    command.Parameters.AddWithValue(" @empFirstName", empFirstName);
    command.Parameters.AddWithValue("@empMidName", empMidName);

    command.Parameters.AddWithValue("@empLastName", empLastName);
    command.Parameters.AddWithValue("@empAge", empAge);
    command.Parameters.AddWithValue(" @empTitle", empTitle);
    command.Parameters.AddWithValue("@empAddress", empAddress);
    command.Parameters.AddWithValue("@empRank", empRank);
    command.Parameters.AddWithValue("@empSalary", empSalary);
    command.Parameters.AddWithValue(" @empEmail", empEmail);
    command.Parameters.AddWithValue("@empPhone", empPhone);
    command.Parameters.AddWithValue("@empMobile", empMobile);
    command.Parameters.AddWithValue("@Notes", Notes);
    command.Parameters.AddWithValue("@userName", userName);
    command.Parameters.AddWithValue("@usrPassword", usrPassword);
     command.Parameters.AddWithValue("@usrAccessLevel", usrAccessLevel);
    command.Parameters.AddWithValue("@empActive", empActive);
    command.Parameters.AddWithValue("@empMarked", empMarked);
    command.Parameters.AddWithValue("@empType", empType);
     connection.Open();
      int result = command.ExecuteNonQuery();

    // Check Error
       if(result < 0)
        Console.WriteLine("Error inserting data into Database!");
      }
  }

【讨论】:

  • 谢谢,但是它给了我这样的错误: System.ArgumentException: 'No mapping exists from object type System.Windows.Forms.TextBox to an known managed provider native type.'
  • @Khalid 告诉我什么是错误
  • System.ArgumentException: '不存在从对象类型 System.Windows.Forms.TextBox 到已知托管提供程序本机类型的映射。'
  • 您只需要验证右侧的所有值都有效,请参阅stackoverflow.com/questions/9543635/…
  • 只需检查所有值
【解决方案2】:

@trailmax 是对的——对于 SQL,您需要在列名列表周围加上括号,以将它们与前后的“INSERT INTO”和“VALUES”部分分开,如下所示:

INSERT INTO tblEmployees (cid, empID, empFirstName, empMidName,empLastName, empAge, empTitle, empAddress, empRank, empSalary, empEmail, empPhone,empMobile, Notes, userName, usrPassword, usrAccessLevel, empActive, empMarked, empType) VALUES (
[...]

【讨论】:

    【解决方案3】:

    我发现通过使用 string.format 为查询创建一个字符串来调试这样的代码很容易。您可以获取字符串结果并将其放入 SQL Server Management Studio (SSMS),它具有更好的诊断功能以帮助查找问题。

                string query = string.Format("INSERT INTO tblEmployees cid, empID, empFirstName, empMidName, " +
                               "empLastName, empAge, empTitle, empAddress, empRank, empSalary, empEmail, empPhone, " +
                               "empMobile, Notes, userName, usrPassword, usrAccessLevel, empActive, empMarked, empType " +
                               "VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}'," +
                                       "'{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}')",
                                       this.txtID, this.txtEmpID, this.txtFirstName, this.txtMidName, this.txtLastName,
                                       this.txtEmpAge, this.txtJobTitle, this.txtAddress, this.cmbRank, this.txtSalary,
                                       this.txtEmail, this.txtPhone, this.txtMobile, this.txtNote, this.txtUserName,
                                       this.txtPassword, this.cmbAcsLevel, this.txtActive, this.txtMarked, this.txtType);
    

    【讨论】:

    • 也许是一个很好的提示,但 IMO 这绝不是问题的答案。
    • 我的代码可能解决了这个问题,因为您可以很容易地看到所有逗号和单引号都在正确的位置。
    • 然而,这仍然容易受到 Sql Injection 的影响。更好地使用参数并获得两全其美!
    猜你喜欢
    • 2022-06-11
    • 2019-12-22
    • 2015-03-25
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    相关资源
    最近更新 更多