我有两张桌子。
客户(int id,nvachar name(250),int age)
Bonus (int id,int customer_id引用Customers(id),int someAmount)
我需要添加,删除,更新有关客户和奖金的信息。
当我添加新客户时,programm会自动在表Bonus中创建一个关于此客户的新条目 当我删除客户时,程序员在表Bonus中删除关于该客户的条目。
将其保存到sql数据库时出现问题。
我有这些SQL命令:
//Bonus
SqlCommand inscmdT = new SqlCommand();
inscmdT.CommandText = "Insert into Bonus (customer_id, someAmount) values(@customer_id, @someAmount); select id = @@IDENTITY from Bonus";
inscmdT.Connection = conn;
inscmdT.Parameters.Add("@customer_id", SqlDbType.Int, sizeof(Int32), "customer_id");
inscmdT.Parameters.Add("@someAmount", SqlDbType.Int, sizeof(Int32), "someAmount");
SqlCommand updcmdT = new SqlCommand();
updcmdT.CommandText = "UPDATE Bonus SET customer_id = @customer_id, someAmount = @someAmount WHERE id = @id";
updcmdT.Connection = conn;
updcmdT.Parameters.Add("@customer_id", SqlDbType.Int, sizeof(Int32), "customer_id");
updcmdT.Parameters.Add("@someAmount", SqlDbType.Int, sizeof(Int32), "someAmount");
updcmdT.Parameters.Add("@id", SqlDbType.Int, sizeof(Int32), "id");
SqlCommand delcmdT = new SqlCommand();
delcmdT.CommandText = "DELETE FROM Bonus WHERE id = @id";
delcmdT.Parameters.Add("@id", SqlDbType.Int, sizeof(Int32), "id");
delcmdT.Connection = conn;
//Customers
SqlCommand inscmdS = new SqlCommand();
inscmdS.CommandText = "Insert into Customers (SessionTime, movie, hall, age) values(@SessionTime, @age); select id = @@IDENTITY from Customers";
inscmdS.Connection = conn;
inscmdS.Parameters.Add("@SessionTime", SqlDbType.NVarChar, 250, "SessionTime");
inscmdS.Parameters.Add("@age", SqlDbType.Int, sizeof(Int32), "age");
SqlCommand updcmdS = new SqlCommand();
updcmdS.CommandText = "UPDATE Customers SET SessionTime = @SessionTime, age = @age WHERE id = @id ";
updcmdS.Connection = conn;
updcmdS.Parameters.Add("@SessionTime", SqlDbType.NVarChar, 250, "SessionTime");
updcmdS.Parameters.Add("@age", SqlDbType.Int, sizeof(Int32), "age");
updcmdS.Parameters.Add("@id", SqlDbType.Int, sizeof(Int32), "id");
SqlCommand delcmdS = new SqlCommand();
delcmdS.CommandText = "DELETE FROM Customers WHERE id = @id";
delcmdS.Parameters.Add("@id", SqlDbType.Int, sizeof(Int32), "id");
delcmdS.Connection = conn;
如何在删除,插入,更新时正确编写SqlDataAdapter?
答案 0 :(得分:0)
SqlDataAdapter用于根据选择查询填充数据集。这不是你想要的。
相反,继续使用SqlCommand对象,使用引用here的 ExecuteNonQuery()方法。
所以,你会得到如下代码:
//Bonus
SqlCommand inscmdT = new SqlCommand();
inscmdT.CommandText = "Insert into Bonus (customer_id, someAmount) values(@customer_id, @someAmount); select id = @@IDENTITY from Bonus";
inscmdT.Connection = conn;
inscmdT.Parameters.Add("@customer_id", SqlDbType.Int, sizeof(Int32), "customer_id");
inscmdT.Parameters.Add("@someAmount", SqlDbType.Int, sizeof(Int32), "someAmount");
inscmdT.ExecuteNonQuery();
等
答案 1 :(得分:0)
调用SqlDataAdapter.Update(dataset)
。只要你有正确的命令集。您已经设置的只需要分配
SqlDataAdpater.DeleteCommand = delCMDT;
SqlDataAdapter.UpdateCOmmand = updCMT;
SQlDataAdapter.InsertCommand = incmdT
如果您可以使用Visual Studio创建一个类型化的数据集,那么这将使事情变得更加容易。基于您的数据库架构。我也不确定poara如何使用无类型数据集,因为我总是使用类型化数据集。你可以自己创造非常tediuos
答案 2 :(得分:0)
解决方案是在表Customers上使用级联删除。