【问题标题】:the column name is not valid node name (if any) = , column name = d列名无效节点名(如果有) = , 列名 = d
【发布时间】:2013-04-17 04:01:51
【问题描述】:

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

代码:

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。

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

【问题讨论】:

标签: c# sql-server-2008 sql-insert


【解决方案1】:

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

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

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

【讨论】:

  • 我把代码改成了这个 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);错误更改为:
  • 需要引用的值,而不是列名。
  • 我改成SqlCeCommand cmd = new SqlCeCommand("insert into Staffs(name, age,资格, mobile,座机,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);我没有引用图片,显示另一个错误,请将此代码编辑到右侧。
  • 你必须自己解决这个问题。参数化你的 SQL 语句,给你的文本框命名一些更合理的名字,然后你就可以自己追踪问题了。
【解决方案2】:

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

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

【讨论】:

  • 我尝试在没有 pic 列的情况下插入,但错误仍然出现。
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多