gpt4 book ai didi

c# - Number of query values and destination fields are not the same 错误

转载 作者:太空狗 更新时间:2023-10-29 23:17:41 26 4
gpt4 key购买 nike

我在将数据插入数据库时​​遇到错误。

错误是:

"Number of query values and destination fields are not the same".

插入代码:

OleDbConnection vconn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Mutyyba\\Documents\\Database1.accdb");
vconn.Open();

string name = textBox1.Text;
string address = textBox3.Text;
int rollno = Convert.ToInt32(textBox2.Text);

string vquery = "insert into Table1 values(@vname,@vrollno,@vaddress)";

OleDbCommand vcomm = new OleDbCommand(vquery, vconn);
vcomm.Parameters.AddWithValue("@vname", name);
vcomm.Parameters.AddWithValue("@vrollno", rollno);
vcomm.Parameters.AddWithValue("@vaddress", address);

vcomm.ExecuteNonQuery();

MessageBox.Show("your record has been recorded sucessfully!");

vconn.Close();

我做错了什么?

最佳答案

我认为您只是遗漏了一些单引号。我看到您已经用起始和结束单引号将所有参数括起来。参见 this

还有一点,当您传递大量参数时,请为参数准备一个SqlCommand 对象。参见 msdn了解更多详情。

做这样的事情:

  SqlCommand comm = new SqlCommand("INSERT INTO table VALUES (@txtsno, @txtdesg, @txtbasic)", connection);

comm.Parameters.AddWithValue("@txtsno", txtsno.Text.Trim());

comm.Parameters.AddWithValue("@txtsno", txtdesg.Text.Trim());

comm.Parameters.AddWithValue("@txtsno", txtbasic.Text.Trim());

这样会更清晰,不会容易出现SQL Injection.

关于c# - Number of query values and destination fields are not the same 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8046238/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com