如何将一条记录插入数据库中的多个表中
本文关键字:数据库 插入 一条 记录 | 更新日期: 2023-09-27 18:00:55
我在C#和SQL Server 2005 中自学
我在SQLServer2005中有3个表,在C#中有一个表单,它接受要保存在数据库中的输入。
我的3张桌子是
- 列为
Hotel No, Hotel Name, Location
的Hotel
- 列为
Guest No, Guest Name, Address
的Guest
- 列为
Room No Room Type, Room Price
的Room
以及一个连接表,所有三个表都符合
Booking
Hotel No, Guest No, Room No, Date From, Date To
我设置了酒店号、客人号、房间号是预订表的外键的关系
在我的C#表单中,我有一个代码,它填充了酒店名称和房间类型的组合框
private void FillComboBoxHotelName()
{
conn.Open();
SqlCommand cmd1 = new SqlCommand("SELECT * FROM Hotel", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd1);
da.SelectCommand.CommandText = cmd1.CommandText.ToString();
DataTable dt = new DataTable();
da.Fill(dt);
cmbHotelName.DataSource = dt;
cmbHotelName.DisplayMember = "Hotel Name";
cmbHotelName.ValueMember = "HotelNo";
conn.Close();
}
private void FillComboBoxRoomType()
{
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Room", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand.CommandText = cmd.CommandText.ToString();
DataTable dt = new DataTable();
da.Fill(dt);
cmbRoomType.DataSource = dt;
cmbRoomType.DisplayMember = "Room Type";
cmbRoomType.ValueMember = "RoomNo";
conn.Close();
}
这个代码有效,我在Hotel和Room中的表中的记录填充了我表单中的组合框,我现在的问题是如何将表单中的输入值存储到我的数据库中。我是否需要为每个表创建一个单独的Insert查询命令,因为数据将进入3个表,我不知所措。
我的表格是这样的:
Hotel Name : Combo Box
Guest Name:
Guest Address:
Date From:
Date To:
Room Type:
Button (Save)
请帮忙,我仍然无法发布图片。
有两种方法可以实现插入语句和存储过程。
插入
string sql = "INSERT INTO Hotel (Hotel No, Hotel Name, Location) values (@HotelNo,@HotelName,@Location)";
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@HotelNo", SqlDbType.Int);
cmd.Parameters.Add("@HotelName", SqlDbType.VarChar);
cmd.Parameters.Add("@Location", SqlDbType.VarChar);
cmd.Parameters["@HotelNo"].Value = HotelNo; // Hotel number name in Int
cmd.Parameters["@HotelName"].Value = HotelName;//Hotel Name in string
cmd.Parameters["@Location"].Value = Location;//Location in form of text
cmd.ExecuteNonQuery();
注意:在代码中,我假设Hotel No是Int,Hotel Name是VarChar,Location是VarChar。
以类似的方式,您可以为剩下的两个表创建Insert语句,并将代码插入到两个表中。
存储过程
为要插入数据库的所有列创建存储过程。像
CREATE PROCEDURE yourInsertOperation
-- Add the parameters for the stored procedure here
@HotelNo INT,
@HotelName VARCHAR(20),
@Location VARCHAR(20),
--......rest of all your parameter list goes here
AS
BEGIN
INSERT INTO resourceTable(column1, column2) VALUES (@HotelNo, @HotelName, @Location )
--Other 2 insert statements will go here
End
Go
存储过程在数据库中就位后。您可以调用该存储过程以及C#代码中的参数。
SqlCommand cmd = new SqlCommand("yourInsertOperation",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@HotelNo", SqlDbType.Int);
cmd.Parameters.Add("@HotelName", SqlDbType.VarChar);
cmd.Parameters.Add("@Location", SqlDbType.VarChar);
//Other columns to insert will go here
cmd.Parameters["@HotelNo"].Value = HotelNo; // Hotel number name in Int
cmd.Parameters["@HotelName"].Value = HotelName;//Hotel Name in string
cmd.Parameters["@Location"].Value = Location;//Location in form of text
//Values for columns will go here
cmd.ExecuteNonQuery();
总之,我建议您使用Insert查询,因为它将简单快捷地插入数据库。在这方面,你不必努力创建SP。但你仍然可以选择使用任何一种或两种方法来实现学习目的。
有多种方法可以解决您的问题:
-
在插入表Booking时创建触发器,该触发器将填充其他相关表
-
在C#中为每个涉及的表插入SQL,这不是最好的方法,但它是可以做到的。
-
构建存储过程,以便按顺序在所有4个表中进行插入。使用C#代码调用此SP。
我更喜欢按偏好使用3个或1个选项。