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



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

代码:

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:Usersadmindocumentsvisual studio 2010ProjectsWindowsFormsApplication1WindowsFormsApplication1hotel.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"。

如果我在文本字段中输入数字而不是字符,错误会变成这样"缺少一个参数[Parameter序数= 1]。列的数据类型为nvarchar。

我尝试编辑数据库模式,但没有发生任何事情。

我检查了数据库的副本,没有发现,所以我想问题出在代码上。

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

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

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

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

这个查询尝试运行,当然它失败了。SQL参数消除了对单引号的需要,除了使您的代码更安全,更容易阅读。

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

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

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

显然,如果数据库中的表版本比您想要使用的版本老,那么该列在该表中不存在,就会发生这种情况

最新更新