C# 尝试插入我的数据失败
我试图将一些列表插入我的数据库(Microsoft),但出现了此运行时错误 无法将参数值从列表“1”转换为Int32 这是我的密码C# 尝试插入我的数据失败,c#,mysql,winforms,runtime-error,runtimeexception,C#,Mysql,Winforms,Runtime Error,Runtimeexception,我试图将一些列表插入我的数据库(Microsoft),但出现了此运行时错误 无法将参数值从列表“1”转换为Int32 这是我的密码 public void InsertInventory(DateTime _date, int _customer_Id, int _employee_Id, List<int> _product_Id, List<int> _
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();
}
public void InsertInventory(DateTime\u date,int\u customer\u Id,
内部员工Id、列表产品Id、,
列出已售出的数量,
清单(单价、清单(总价)
{
连接字符串=@“数据源=MOSTAFA-PC;初始目录=”
+“销售和库存系统”
+“集成安全性=TrueData源=MOSTAFA-PC;初始目录=”
+“销售和库存系统”
+“综合安全=真实;”;
Query=“插入库存”+
(客户Id、员工Id、产品Id、[日期]、[销售金额]、[单价]、[总价])+
“值(@customer\u id、@Employee\u id、@Product\u id、@[日期]、@[销售金额]、@[单价]、@[总价])”;
使用(Con=newsqlconnection(连接\字符串))
使用(Cmd=newsqlcommand(Query,Con))
{
Cmd.Parameters.Add(“@customer\u 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\u sell]”,SqlDbType.Int);
Cmd.Parameters.Add(“@[单价]”,SqlDbType.Decimal);
Cmd.Parameters.Add(“@Total_Price”,SqlDbType.Decimal);
Cmd.Connection=Con;
Con.Open();
int RecordToAdd=\u product\u Id.Count;
for(int i=0;i
我搜索了这些网站,但找不到任何有用或类似于我的问题的东西
我该怎么办?更改Int32或Integer函数参数中的“列表”。更改此选项:
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=\u product\u Id.Count;
for(int i=0;i
为此:
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();
}
int RecordToAdd=\u product\u Id.Count;
for(int i=0;i
注意,在第二个块中,我使用循环的迭代器来索引每次插入的列表中的哪个项
编辑:此外,您构建的连接字符串看起来不正确。您可能希望查看它并更正任何错误似乎您正在尝试将列表插入到一个预期为和整数的参数中。如果您尝试的是基于列表的批量插入,则应使用for循环中的i迭代器作为索引号to列表。
Cmd.Parameters[“@Product\u id”].Value=\u Product\u id[i];
是涉及列表的一个示例。