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

编程入门 行业动态 更新时间:2024-10-25 02:27:53
本文介绍了windows窗体应用程序中的列名无效如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

按钮点击事件

in button click event

private void button3_Click(object sender, EventArgs e) { SqlStr = String.Format("insert into employee(empid,ename,salary,deptno) values ({0},{1},{2},{3})", textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text); ExecuteDML(); button3.Enabled = false; }

命令执行方法我写了这样的代码

in command exection method i have written the code like this

private void ExecuteDML() { DialogResult d = MessageBox.Show("Are you sure of executing the below SQl Statement?\n\n" + SqlStr, "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if(d==DialogResult.Yes) { cmd.CommandText = SqlStr; int Count = cmd.ExecuteNonQuery(); if(Count>0) { MessageBox.Show("Statement executed Successfully"); } else { MessageBox.Show("Statement failed execution"); } LoadData(); } }

但我在执行代码时收到无效的列名错误。 有人帮我谢谢。

but i am getting the invalid column name error while executing the code. someone help me thanks.

推荐答案

基本上是因为你从文本框中添加值而没有开始和结束'。 然而更大的问题是你不使用参数。 修正这个问题的正确方法是使用 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.

更多推荐

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

本文发布于:2023-10-04 03:43:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467111.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗体   如何解决   应用程序   windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!