困惑关于如何将xml值插入数据库

时间:2013-08-18 19:05:35

标签: asp.net sql sql-server xml

我正在处理xml文件,我找到了一个示例here。我更改了连接字符串并创建了一个名为MyProducts的表,然后我手动将我的Product.xml文件放在App_Data文件夹中。当我运行我的时候程序获得此执行

  

无效的对象名称'Product'。

所以在调试模式中我注意到myxml变量为null我做错了什么

              protected void Button1_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;
        double product_Price = 0;

        connetionString = "Data Source=.\\sqlexpress;Initial Catalog=Northwind;Integrated Security=sspi";

        connection = new SqlConnection(connetionString);


        xmlFile = XmlReader.Create(Server.MapPath("~/App_Data/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 = 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();
        Label1.Text = "ok";

    }

1 个答案:

答案 0 :(得分:1)

正如我在评论中所说的那样,你刚刚错误拼写了你的表名 - 你的表名为MyProducts,你试图插入产品

insert into MyProducts ...