插入数据到数据库错误参数
Insert into data to database Error Parameters
我想将我的 DataGridView 数据插入到我使用参数的数据库中
这些是我的数据库文件
CREATE TABLE [dbo].[Tbl_Order] (
[Name] VARCHAR (50) NULL,
[Price] INT NULL,
[Quantity] INT NULL,
[Total] INT NULL
);
这些是我的代码
SqlConnection con = new SqlConnection("Data Source=DESKTOP-R34C6VV\SQL;Initial Catalog=Student_Databse_2019_HW1;Integrated Security=True");
con.Open();
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++ )
{
SqlCommand cmd = new SqlCommand("INSERT INTO Tbl_Order(Name, Price, Quantity, Total) values(@Name, @Price, @Qty, @Total",con);
cmd.Parameters.AddWithValue("@Name", dataGridView1.Rows[i].Cells[0].Value);
cmd.Parameters.AddWithValue("@Price", dataGridView1.Rows[i].Cells[1].Value);
cmd.Parameters.AddWithValue("@Qty", dataGridView1.Rows[i].Cells[2].Value);
cmd.Parameters.AddWithValue("@Total", dataGridView1.Rows[i].Cells[3].Value);
cmd.ExecuteNonQuery();
}
con.Close();
dataGridView1.Rows.Clear();
我对这段代码有错误 cmd.ExecuteNonQuery();
它说
'System.Data.SqlClient.SqlException' 类型的未处理异常发生在 System.Data.dll
附加信息:“@Total”附近的语法不正确。
我的 DataGridView 中有 4 个字段
Name , Price , Qty , Total
有人知道是什么问题吗?
您没有在 SQL 声明中关闭圆括号。
从您的代码看来,您的 Insert 语句中似乎缺少一个“)”。
我想将我的 DataGridView 数据插入到我使用参数的数据库中
这些是我的数据库文件
CREATE TABLE [dbo].[Tbl_Order] (
[Name] VARCHAR (50) NULL,
[Price] INT NULL,
[Quantity] INT NULL,
[Total] INT NULL
);
这些是我的代码
SqlConnection con = new SqlConnection("Data Source=DESKTOP-R34C6VV\SQL;Initial Catalog=Student_Databse_2019_HW1;Integrated Security=True");
con.Open();
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++ )
{
SqlCommand cmd = new SqlCommand("INSERT INTO Tbl_Order(Name, Price, Quantity, Total) values(@Name, @Price, @Qty, @Total",con);
cmd.Parameters.AddWithValue("@Name", dataGridView1.Rows[i].Cells[0].Value);
cmd.Parameters.AddWithValue("@Price", dataGridView1.Rows[i].Cells[1].Value);
cmd.Parameters.AddWithValue("@Qty", dataGridView1.Rows[i].Cells[2].Value);
cmd.Parameters.AddWithValue("@Total", dataGridView1.Rows[i].Cells[3].Value);
cmd.ExecuteNonQuery();
}
con.Close();
dataGridView1.Rows.Clear();
我对这段代码有错误 cmd.ExecuteNonQuery();
它说 'System.Data.SqlClient.SqlException' 类型的未处理异常发生在 System.Data.dll 附加信息:“@Total”附近的语法不正确。
我的 DataGridView 中有 4 个字段
Name , Price , Qty , Total
有人知道是什么问题吗?
您没有在 SQL 声明中关闭圆括号。
从您的代码看来,您的 Insert 语句中似乎缺少一个“)”。