2013-08-18 42 views
0

我正在處理xml文件,我找到了一個示例here。我更改了連接字符串並創建了一個名爲MyProducts的表,然後我手動將我的Product.xml文件放入了App_Data文件夾。當我運行我的程序得到這個執行困惑關於如何將xml值插入到數據庫中

無效的對象名稱'產品'。

所以在調試模式我已經注意到,myxml變量爲空我在做什麼錯

   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"; 

    } 
+0

你表中調用我的產品和你想插入產品 –

+0

謝謝羅馬我想我一定好好睡一覺 – user2583511

+1

[SQL注入警報(HTTP ://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - 您應該**不**將您的SQL語句連接在一起 - 使用**參數化查詢**而不是避免SQL注入 –

回答

1

正如我在評論中已經說過了,你剛剛輸入錯誤的表名 - 你的表我的產品叫和你想插入產品

insert into MyProducts ...