SqlDataAdapter problems

本文关键字:problems SqlDataAdapter | 更新日期: 2023-09-27 17:56:04

我有两个表。

客户(整数 ID、nvachar 名称 (250)、整数年龄)

奖金(int id,int customer_id引用客户(id),int someAmount)

我需要添加、删除、更新有关客户和奖金的信息。

当我添加一个新客户时,程序会自动在表奖金中创建有关该客户的新条目。
当我删除客户时,程序删除表中有关该客户的条目奖金。

将其保存到 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?

SqlDataAdapter problems

SqlDataAdapter 用于基于选择查询填充数据集。这似乎不是你要找的。

相反,继续使用 SqlCommand 对象,使用此处引用的 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();

等。

更改

数据集后调用SqlDataAdapter.Update(dataset)。 只要您设置了正确的命令。 您已经设置好了 只需要分配

SqlDataAdpater.DeleteCommand = delCMDT;
SqlDataAdapter.UpdateCOmmand = updCMT;
SQlDataAdapter.InsertCommand = incmdT

如果你可以使用Visual Studio创建一个类型化数据集,那会比为你完成所有这些工作要容易得多。 基于数据库架构。 我也不确定 Poarams 如何处理非类型化数据集,因为我总是使用类型化数据集。 你可以让自己虽然很乏味

SqlDataAdapter

解决方案是对表客户使用级联删除。