2016-01-23 69 views
0

我试图插入一些名单到我的数据库(微软),但我得到这个运行时错误失败试图插入我的数据

未能参数值从List`1转换成的Int32 。

这里是我的代码

public void InsertInventory(DateTime _date, int _customer_Id, 
          int _employee_Id, List<int> _product_Id, 
          List<int> _amountSold, 
          List<int> _unitPrice, List<int> _totalPrice) 
    { 
     Connection_String = @"Data Source=MOSTAFA-PC;Initial Catalog=" 
          + "Sales and Inventory System" 
          + ";Integrated Security=TrueData Source=MOSTAFA-PC;Initial Catalog=" 
          + "Sales and Inventory System" 
          + ";Integrated Security=True;"; 

     Query = "insert into Inventory" + 
        "(Customer_Id,Employee_Id,Product_Id,[Date],[Amount Sold],[Unit Price],[Total Price])" + 
        "values (@customer_id,@Employee_id,@Product_id,@[Date],@[Amount_Sold],@[Unit_Price],@[Total_Price])"; 

     using (Con = new SqlConnection(Connection_String)) 
     using (Cmd = new SqlCommand(Query, Con)) 
     { 
      Cmd.Parameters.Add("@customer_id", SqlDbType.Int); 
      Cmd.Parameters.Add("@Employee_id", SqlDbType.Int); 
      Cmd.Parameters.Add("@Product_id", SqlDbType.Int); 
      //Cmd.Parameters.Add("@[Date]", SqlDbType.NVarChar); 
      Cmd.Parameters.Add("@[Date]", SqlDbType.Date); 
      Cmd.Parameters.Add("@[Amount_sold]", SqlDbType.Int); 
      Cmd.Parameters.Add("@[Unit_Price]", SqlDbType.Decimal); 
      Cmd.Parameters.Add("@Total_Price", SqlDbType.Decimal); 

      Cmd.Connection = Con; 
      Con.Open(); 

      int RecordToAdd = _product_Id.Count; 
      for (int i = 0; i < RecordToAdd; i++) 
      { 
       Cmd.Parameters["@customer_id"].Value = _customer_Id; 
       Cmd.Parameters["@Employee_id"].Value = _employee_Id; 
       Cmd.Parameters["@Product_id"].Value = _product_Id; 
       Cmd.Parameters["@[Date]"].Value = _date; 
       Cmd.Parameters["@[Amount_sold]"].Value = _amountSold; 
       Cmd.Parameters["@[Unit_Price]"].Value = _unitPrice; 
       Cmd.Parameters["@Total_Price"].Value = _totalPrice; 
       Cmd.ExecuteNonQuery(); 
      } 

我搜索的网站,但我找不到任何有用的东西或类似我的问题

我应该怎么做?

+0

您似乎试图将列表插入到期望的整数参数中。如果你想要做的是基于列表的批量插入,你应该使用for循环中的i迭代器作为列表的索引号。 'Cmd.Parameters [“@ Product_id”]。Value = _product_Id [i];'是涉及列表的一个示例。 –

回答

0

变化这样的:

 int RecordToAdd = _product_Id.Count; 
     for (int i = 0; i < RecordToAdd; i++) 
     { 
      Cmd.Parameters["@customer_id"].Value = _customer_Id; 
      Cmd.Parameters["@Employee_id"].Value = _employee_Id; 
      Cmd.Parameters["@Product_id"].Value = _product_Id; 
      Cmd.Parameters["@[Date]"].Value = _date; 
      Cmd.Parameters["@[Amount_sold]"].Value = _amountSold; 
      Cmd.Parameters["@[Unit_Price]"].Value = _unitPrice; 
      Cmd.Parameters["@Total_Price"].Value = _totalPrice; 
      Cmd.ExecuteNonQuery(); 
     } 

向该:在我使用循环的迭代索引我插入每个列表中的项目的第二块

 int RecordToAdd = _product_Id.Count; 
     for (int i = 0; i < RecordToAdd; i++) 
     { 
      Cmd.Parameters["@customer_id"].Value = _customer_Id; 
      Cmd.Parameters["@Employee_id"].Value = _employee_Id; 
      Cmd.Parameters["@Product_id"].Value = _product_Id[i]; 
      Cmd.Parameters["@[Date]"].Value = _date; 
      Cmd.Parameters["@[Amount_sold]"].Value = _amountSold[i]; 
      Cmd.Parameters["@[Unit_Price]"].Value = _unitPrice[i]; 
      Cmd.Parameters["@Total_Price"].Value = _totalPrice[i]; 
      Cmd.ExecuteNonQuery(); 
     } 

通知时间。

编辑:此外,您建立的connection_string看起来不正确。您可能想要查看它并更正任何错误

0

更改功能参数中Int32或Integer的“List <>”。

+0

它没有工作。 – NeverTrust