我有兩個表。SqlDataAdapter問題
客戶(INT ID,nvachar名(250),INT年齡)
獎金(INT ID,INT CUSTOMER_ID參照客戶(ID),INT someAmount)
我需要添加,刪除,更新有關客戶和獎金的信息。
當我添加一個新客戶時,程序會自動在表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,更新嗎?
你使用Visual Studio嗎? – RadioSpace