C# 尝试插入数据时出现运行时错误
我试图将数据插入microsoft数据库,但引发了此运行时错误 “日期”附近的语法不正确。\r\n必须声明标量变量\“@\” 在这一行中C# 尝试插入数据时出现运行时错误,c#,mysql,winforms,runtime-error,runtimeexception,C#,Mysql,Winforms,Runtime Error,Runtimeexception,我试图将数据插入microsoft数据库,但引发了此运行时错误 “日期”附近的语法不正确。\r\n必须声明标量变量\“@\” 在这一行中Cmd.ExecuteNonQuery() 这是我的密码 public void InsertInventory(DateTime _date, int _customer_Id, int _employee_Id, List<int> _product_Id,
Cmd.ExecuteNonQuery()代码>
这是我的密码
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["@[Date]"].Value = _date;
Cmd.Parameters["@Product_id"].Value = _product_Id[i];
Cmd.Parameters["@[Amount_sold]"].Value = _amountSold[i];
Cmd.Parameters["@[Unit_Price]"].Value = _unitPrice[i];
Cmd.Parameters["@Total_Price"].Value = _totalPrice[i];
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
我该怎么办?对于参数名,您不需要用[]换行,您可以使用@Date、@amountsell、@UnitPrice、@TotalPrice。只需确保在语句和参数列表中都修复了它们对于参数名,您不需要用[]换行只需使用@Date、@amountsell、@UnitPrice、@TotalPrice即可。只需确保在语句和参数列表中都修复了它们