且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

windows窗体应用程序中的列名无效如何解决?

更新时间:2022-10-31 18:35:22

基本上是因为你从文本框中添加值而没有开始和结束'。



然而更大的问题是你不使用参数。



修正这个问题的正确方法是使用 SqlParameter [ ^ ]

所以代替

Basically the error comes because you add the values from the text boxes without having starting and ending '.

However the bigger problem is that you don't use parameters.

Correct way to fix this is to use SqlParameter[^]
So instead of
SqlStr = String.Format("insert into employee(empid,ename,salary,deptno) values ({0},{1},{2},{3})", textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
ExecuteDML();



你应该有类似


You should have something like

SqlStr = "insert into employee(empid,ename,salary,deptno) values (@empid,@ename,@salary,@deptno)");
ExecuteDML();



然后使用 AddWithValue [ ^ ]您将参数添加到要使用的命令的参数集合中。


and then using AddWithValue[^] you add the parameters to the parameter collection of the command to be used.