我想創建一個Students
表,Students_Images
表和Photo_Collection
表。圖像路徑存儲在Student_Images
中,每個圖像鏈接到Photo_Collection
行,並且每個Photo_Collection
都與Student
行相關。INSERT到SQL Server表使用連接鍵和外鍵
學生記錄已存在。然後,學生可以爲自己創建照片集,並將照片添加到此照片集。因此,每張圖片都需要與一張照片集合相關聯,並且每張照片集合都與一名學生相關聯。
我被告知要使用下面的代碼:
create table dbo.Students
(
ID
int not null identity(1, 1)
constraint [Students.ID.PrimaryKey]
primary key clustered,
Name
nvarchar(50)
)
create table dbo.Student_Images
(
ID
int not null identity(1, 1)
constraint [Student_Images.ID.PrimaryKey]
primary key clustered,
Student_ID
int not null
constraint [Student_Images.to.Student]
foreign key references dbo.Students(ID)
Filename
nvarchar(250) null,
Description
nvarchar(250) null
)
create table dbo.Photo_Collection
(
ID
int not null identity(1, 1)
constraint [Photo_Collection.ID.PrimaryKey]
primary key clustered,
Name
nvarchar(250) null
)
create table dbo.Photo_Collection_Images
(
Photo_Collection_ID
int not null
constraint [Photo_Collection_Images.to.Photo_Collection]
foreign key references dbo.Photo_Collection(ID),
Student_Image_ID
int not null
constraint [Photo_Collection_Images.to.Student_Images]
foreign key references dbo.Student_Images(ID)
)
下面是填充時,這些表會怎樣看一個例子:
我可以INSERT into Students,
Student_Images
和Photo_Collection
使用以下SQL:
protected void btnAddImage_Click(object sender, EventArgs e)
{
addImage();
}
public void addStudent()
{
string cmdText = "INSERT INTO Students (Name) VALUES (@N)";
//====== Providning information to SQL command object about which query to
//====== execute and from where to get database connection information.
SqlCommand cmd = new SqlCommand(cmdText, con);
//===== Adding parameters/Values.
cmd.Parameters.AddWithValue("@N", txtStudentName.Text.ToString());
//===== To check current state of the connection object. If it is closed open the connection
//===== to execute the insert query.
if (con.State == ConnectionState.Closed)
{
con.Open();
}
//===== Execute Query.
cmd.ExecuteNonQuery();
//===== close the connection.
con.Close();
}
public void addCollection()
{
string cmdText = "INSERT INTO Photo_Collection (Name) VALUES (@N)";
//====== Providning information to SQL command object about which query to
//====== execute and from where to get database connection information.
SqlCommand cmd = new SqlCommand(cmdText, con);
//===== Adding parameters/Values.
cmd.Parameters.AddWithValue("@N", txtPhotoCollectionName.Text.ToString());
//===== To check current state of the connection object. If it is closed open the connection
//===== to execute the insert query.
if (con.State == ConnectionState.Closed)
{
con.Open();
}
//===== Execute Query.
cmd.ExecuteNonQuery();
//===== close the connection.
con.Close();
}
public void addImage()
{
string cmdText = "INSERT INTO Student_Images (Student_Id, Filename, Description) VALUES (@S, @F, @D)";
//====== Providning information to SQL command object about which query to
//====== execute and from where to get database connection information.
SqlCommand cmd = new SqlCommand(cmdText, con);
//===== Adding parameters/Values.
cmd.Parameters.AddWithValue("@S", ddlStudentNames.SelectedValue);
cmd.Parameters.AddWithValue("@F", fuImage.FileName.ToString());
cmd.Parameters.AddWithValue("@D", txtImageDescription.Text.ToString());
//===== To check current state of the connection object. If it is closed open the connection
//===== to execute the insert query.
if (con.State == ConnectionState.Closed)
{
con.Open();
}
//===== Execute Query.
cmd.ExecuteNonQuery();
//===== close the connection.
con.Close();
}
protected void btnAddStudentAndCollection_Click(object sender, EventArgs e)
{
addStudent();
addCollection();
}
}
我需要從數據庫中檢索,這樣的事情:
但我不知道該怎麼INSERT
和SELECT
使用Photo_Collection_Images
表,因爲我不知道怎麼樣引用我需要的外鍵以便將這些錶鏈接在一起。
任何人都可以告訴我如何成功地創建照片集合並添加圖像嗎?
我發現這個困難的原因是因爲我必須加入一些表格。
您不必擔心。數據庫表配置完成後,數據庫將自動查找外鍵。數據庫使用insert語句中的列進行映射。添加/插入時,您必須確保主鍵列不爲空。 – jdweng
@jdweng那麼你是說'INSERT INTO Student'和'INSERT INTO Student_Images'語句後,這兩行的PK將被添加到Photo_Collection_Images表中? – Sweg
@jdweng我上面發佈了我的INSERT語句,它們正確地插入Student,Student_Images和Photo_Collection,但目前沒有任何內容插入到Photo_Collection_Images中 – Sweg