繁体   English   中英

无效的列名称:“ value”-错误,即使它以另一种形式工作也是如此。

[英]Invalid Column Name: “value” - Error Even though it works in another form.

我陷入一个问题,但我解决不了。 我收到此错误:

错误信息

那是相关的表

编码:

SqlConnection connection = new SqlConnection(connectionString);
        connection.Open();
        string query = "UPDATE CAC SET nextMaintainance = @nextMaintainance WHERE department = " + @departmentCB.Text;
        SqlCommand command = new SqlCommand(query, connection);
        command.Parameters.AddWithValue("@nextMaintainance", nextMaintainanceDT.Value);
        command.ExecuteNonQuery();

我不明白的是,类似的代码可以正常工作而在我的项目中没有任何错误:

query = "UPDATE LDV SET received = @received, department = @department WHERE Id =" + @idTxt.Text;
            command = new SqlCommand(query, connection);
            command.Parameters.AddWithValue("@received", inDT.Value);
            command.Parameters.AddWithValue("@department", departmentCb.Text);
            command.ExecuteNonQuery();
            MessageBox.Show("Lungenautomat wurde aktualisiert");

如果相关,我的连接字符串:

connectionString = ConfigurationManager.ConnectionStrings["SCBA_Manager_0._1.Properties.Settings.SCBAmanagerConnectionString"].ConnectionString;

我真的希望你能帮助我:(谢谢!

department列是文本列,因此将其与一个值进行比较意味着该值应该用引号引起来。

// This fix is not the recommended approach, see the explanation after this code block  
string query = "UPDATE CAC SET nextMaintainance = @nextMaintainance WHERE department = '" + departmentCB.Text + "'";
                                                                                   //  ^--------------------------^------ single quote added to wrap the value returned by departmentCB.Text 

另一方面,在第二个示例中不会发生此错误,因为在那里您正确使用了Parameters.AddWithValue()方法来添加@department参数的值,并且因为id是一个数字列,所以它不会不需要用引号引起来的值。

但是,尽管上面显示的代码可以完成任务,但这并不是正确的方法。 正确的方法是对所有要插入查询的值使用参数。 您上面显示的查询已经正确地使用了某些值的参数(例如,第一个查询中的nextMaintenance ,第二个中received和第二个department ),但是对于其他值(例如第一个查询中的departmentid in第二)。

参数化SQL的用法

使用参数化SQL的好处是它会自动处理添加引号,防止SQL注入等问题。

因此,最好将您的第一个代码块更改为:

SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
string query = "UPDATE CAC SET nextMaintainance = @nextMaintainance WHERE department = @department";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@department", departmentCb.Text);
command.Parameters.AddWithValue("@nextMaintainance", nextMaintainanceDT.Value);
command.ExecuteNonQuery(); 

请注意,字符串query如何是一个没有任何混乱连接的单个字符串,并且它包含两个参数@nextMaintenance@department 以及如何在下面几行中使用Parameters.AddWithValue()正确注入这些参数的值?

通过为Id列使用参数,可以类似地改善第二个代码块。

query = "UPDATE LDV SET received = @received, department = @department WHERE Id = @Id ";
command.Parameters.AddWithValue("@Id", idTxt.Text);

更多信息

请阅读有关SQL注入的内容( https://technet.microsoft.com/zh-cn/library/ms161953(v=sql.105).aspx ),以了解如何像原始代码一样使用字符串连接会导致各种安全问题,以及为什么参数化查询是将动态值注入SQL查询的首选方式。

您可以在此处阅读有关参数化查询的更多信息: https : //msdn.microsoft.com/zh-cn/library/yy6y35y8(v=vs.110).aspx

在您的第一个示例中,WHERE子句的计算结果为

WHERE department = Kasseedorf

应该是什么

WHERE department = 'Kasseedorf'

所以这条线应该是

string query = "UPDATE CAC SET nextMaintainance = @nextMaintainance WHERE department = '" + @departmentCB.Text +"'";

它在第二个示例中有效,因为id是整数,并且不加引号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM