我有两张桌子。
客户(int id,nvachar名称(250),int年龄)
奖金(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?
发布于 2014-05-18 14:52:44
解决方案是对表客户使用级联删除。
发布于 2014-04-01 17:31:10
SqlDataAdapter用于根据select查询填充数据集。这不是你想要的。
相反,使用引用的ExecuteNonQuery()方法继续使用SqlCommand对象。
因此,您的代码如下所示:
//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();等。
发布于 2014-04-01 17:40:52
更改数据集后调用SqlDataAdapter.Update(dataset)。只要你有正确的命令设置。你已经设置好了只需要分配
SqlDataAdpater.DeleteCommand = delCMDT;
SqlDataAdapter.UpdateCOmmand = updCMT;
SQlDataAdapter.InsertCommand = incmdT如果您可以使用Visual创建类型化数据集,这将比为您完成的所有工作要容易得多。基于您的数据库架构。另外,我也不确定poarams是如何处理非类型化数据集的,因为我总是使用类型化数据集。你可以把你自己做得很好
SqlDataAdapter
https://stackoverflow.com/questions/22792438
复制相似问题