运行 尝试插入数据时出现时间错误
Run time error while trying to insert data
我正在尝试将数据插入我的 Microsoft 数据库,但抛出了这个 运行 时间错误
"Incorrect syntax near 'date'.\r\nMust declare the scalar variable \"@\"."
这一行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();
}
}
我该怎么办?
对于参数名称,您不需要用[] 包裹,您可以使用@Date、@AmountSold、@UnitPrice、@TotalPrice。只需确保在语句和参数列表中修复它们
我正在尝试将数据插入我的 Microsoft 数据库,但抛出了这个 运行 时间错误
"Incorrect syntax near 'date'.\r\nMust declare the scalar variable \"@\"."
这一行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();
}
}
我该怎么办?
对于参数名称,您不需要用[] 包裹,您可以使用@Date、@AmountSold、@UnitPrice、@TotalPrice。只需确保在语句和参数列表中修复它们