C#:列名或提供的值数与表定义不匹配。 [英] C# :column name or number of supplied values does not match table definition.

查看:150
本文介绍了C#:列名或提供的值数与表定义不匹配。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要做的是将数据从XML文件上传到sql数据库。

我的XML文件看起来像这样。

Hi ,
What im trying to do is upload data from an XML File to an sql Database.
My XML File looks like this.

<?xml version="1.0" encoding="utf-8"?>
<Table>
	<Product>
		<Product_id>1</Product_id>
		<Product_name>Product 1</Product_name>
		<Product_price>1000</Product_price>
	</Product>
....





我的数据库列名是Id [int],ProductName [nvarchar50],ProductDescription [nvarchar4000]和其他一些列。



ERROR即时获取



My Database Column Names are Id[int], ProductName[nvarchar50] , ProductDescription[nvarchar4000] and a number of other columns.

The ERROR im getting

Column name or number of supplied values does not match table definition.





我的尝试:





What I have tried:

private void button3_Click(object sender, EventArgs e)
     {
         string connetionString = null;
         SqlConnection connection;
         SqlCommand command;
         SqlDataAdapter adpter = new SqlDataAdapter();
         DataSet ds = new DataSet();
         XmlReader xmlFile;
         string sql = null;

         int product_ID = 0;
         string Product_Name = null;
        string product_Price = null;

         connetionString = "Data Source=server name;Initial Catalog=database;User ID=user;Password=password";

         connection = new SqlConnection(connetionString);

         xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings());
         ds.ReadXml(xmlFile);
         int i = 0;
         connection.Open();
         for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
         {
             product_ID = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]);
             Product_Name = ds.Tables[0].Rows[i].ItemArray[1].ToString();
             product_Price = ds.Tables[0].Rows[i].ItemArray[2].ToString();
                 //Convert.ToDouble(ds.Tables[0].Rows[i].ItemArray[2]);
             sql = "insert into Product values(" + product_ID + ",'" + Product_Name + "'," + product_Price + ")";
             command = new SqlCommand(sql, connection);
             adpter.InsertCommand = command;
             adpter.InsertCommand.ExecuteNonQuery();
         }
         connection.Close();
         MessageBox.Show("Done .. ");
     }
 }

推荐答案

错误有点不言自明,是您提供的插入值与数据库中的列数不匹配。

请检查数据库中的列数或处理此类问题的好方法是,在插入查询中使用列名

见下面的查询

Error is bit self-explanatory, your provided values for insertion does not matched with the number of columns in database.
Please checkout number of columns in database OR the good way to deal with such problem is, Use column name in insert query
see below query
sql = "insert into Product (Id, ProductName, ProductDescription)  values(" + product_ID + ",'" + Product_Name + "'," + product_Price + ")";



在您继续之前,请检查您的ID不应自动递增。


before you proceed, please check your ID should not be auto incremented.


这篇关于C#:列名或提供的值数与表定义不匹配。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆