我试图插入一些名单到我的数据库(微软),但我得到这个运行时错误失败试图插入我的数据
未能参数值从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();
}
我搜索的网站,但我找不到任何有用的东西或类似我的问题
我应该怎么做?
您似乎试图将列表插入到期望的整数参数中。如果你想要做的是基于列表的批量插入,你应该使用for循环中的i迭代器作为列表的索引号。 'Cmd.Parameters [“@ Product_id”]。Value = _product_Id [i];'是涉及列表的一个示例。 –