0

我花了几个小时试图解决这个错误,但我做不到。如果有人能帮我解决这个问题,我会很高兴。

代码:

FileStream fs;
fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read);
byte[] picbyte = new byte[fs.Length];
fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
string query;
SqlCeConnection conn = new SqlCeConnection(@"Data Source=C:\Users\admin\documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\hotel.sdf");
conn.Open();
SqlParameter picparameter = new SqlParameter();
picparameter.SqlDbType = SqlDbType.Image;
picparameter.ParameterName = "pic";
picparameter.Value = picbyte;
query = "insert into Staffs(name, age, qualification, mobile, landline, salary, salary_type, address, work_type, reference, picture) values(" + textBox16.Text + ", " + textBox15.Text + "," + textBox14.Text + "," + textBox13.Text + "," + textBox12.Text + "," + textBox11.Text + "," + comboBox2.Text + "," + richTextBox2.Text + "," + textBox10.Text + "," + textBox9.Text + ", " + " @pic)";
SqlCeCommand cmd = new SqlCeCommand("insert into Staffs(name, age, qualification, mobile, landline, salary, salary_type, address, work_type, reference, picture) values(" + textBox16.Text + ", " + textBox15.Text + "," + textBox14.Text + "," + textBox13.Text + "," + textBox12.Text + "," + textBox11.Text + "," + comboBox2.Text + "," + richTextBox2.Text + "," + textBox10.Text + "," + textBox9.Text + ", " + " @pic)", conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Profile Added");
cmd.Dispose();
conn.Close();
conn.Dispose();

错误:

列名无效节点名(如果有) = , 列名 = d

到目前为止,我发现了什么:

错误中的“column name = d”是文本字段的值。如果我在文本字段中输入 a,错误将变为“列名 = a”。

如果我在文本字段中输入数字而不是字符,则错误会变为“缺少参数 [参数序号 = 1]。列的数据类型是 nvarchar。

我尝试编辑数据库架构,但什么也没发生。

我检查了数据库的重复副本,但没有找到,所以我想问题出在代码上。

这些列的数据类型为 nvarchar、int 或 image。

只是为了确保我检查了数据库以查看插入是否有效,数据库仍然是空的。

4

3 回答 3

2

您需要使用 SQL 参数来避免 SQL 注入问题。

但是你的东西现在失败的原因是你没有在查询中引用你的字符串值。如果您调试并查看命令文本,您会看到类似 的内容values(abc,def,ghi...),而字符串周围没有单引号。

该查询尝试运行,当然它失败了。SQL 参数消除了对单引号的需求,此外还使您的代码更安全、更易于阅读。

于 2013-04-17T04:14:27.553 回答
1

您的参数名称不正确。应该是@pic,不是pic

顺便说一句,您连接文本框值的方式使您容易受到 SQL 注入攻击。当然我不知道你的上下文,但考虑用参数值设置替换字符串连接。

于 2013-04-17T04:09:38.267 回答
0

我通过删除整个数据库并重新创建它来解决这个问题。

显然,如果您在数据库中的表版本比您想要使用的旧版本,则会发生这种情况,因此该表中不存在该列。

于 2014-08-19T09:56:32.590 回答