im创建一个程序,用户可以在其中上传歌曲,并将这些歌曲的元数据(例如其专辑或歌手)记录在关系sql数据库中。我当前的表格设置包含3个表格-Song_DB,Album_DB,Artist_DB。
SONG_DB:
CREATE TABLE [dbo].[SONGS_DB] (
[SONG_ID] INT IDENTITY (1, 1) NOT NULL,
[SONG_NAME] NCHAR (100) NOT NULL,
[ARTIST_ID] INT NULL,
[ALBUM_ID] INT NULL,
[SONG_LENGTH] TIME (0) NOT NULL,
[SONG_PLAYLIST_ID] INT NULL,
PRIMARY KEY CLUSTERED ([SONG_ID] ASC),
CONSTRAINT [FK_SONGS_DB_ToARTIST] FOREIGN KEY ([ARTIST_ID]) REFERENCES [dbo].[ARTIST_DB] ([ARTIST_ID]),
CONSTRAINT [FK_SONGS_DB_ToALBUM] FOREIGN KEY ([ALBUM_ID]) REFERENCES [dbo].[ALBUM_DB] ([ALBUM_ID]),
CONSTRAINT [FK_SONGS_DB_ToSONG_PLAYLIST] FOREIGN KEY ([SONG_PLAYLIST_ID]) REFERENCES [dbo].[SONG_PLAYLIST] ([SONG_PLAYLIST_ID])
Artist_DB
CREATE TABLE [dbo].[ARTIST_DB] (
[ARTIST_ID] INT IDENTITY (1, 1) NOT NULL,
[SONG_ID] INT NULL,
[NAME] TEXT NULL,
PRIMARY KEY CLUSTERED ([ARTIST_ID] ASC),
CONSTRAINT [FK_ARTIST_DB_ToSONGS_DB] FOREIGN KEY ([SONG_ID]) REFERENCES [dbo].[SONGS_DB] ([SONG_ID])
);
相册数据库
CREATE TABLE [dbo].[ALBUM_DB] (
[ALBUM_ID] INT IDENTITY (1, 1) NOT NULL,
[SONG_ID] INT NULL,
[NAME] NCHAR (100) NULL,
PRIMARY KEY CLUSTERED ([ALBUM_ID] ASC),
CONSTRAINT [FK_ALBUM_DB_SONGS] FOREIGN KEY ([SONG_ID]) REFERENCES [dbo].[SONGS_DB] ([SONG_ID])
);
我正在尝试在歌曲上传后将元数据收集到变量中后写入表。我的实际写作虽然有些困难,但是却在努力寻找如何填充外键表的方法。例如,使用以下代码,在Artist / album_DB中都不会填充song_ID,在Song_DB-
中也不会填充Album / artist_ID Dim SQLcon As New SqlConnection With {.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\coolj\OneDrive\Documents\Loop\Loop\Songs_SQL.mdf;Integrated Security=True"}
Dim SQLCmd As SqlCommand
Dim SQLCmd2 As SqlCommand
Dim SQLCmd3 As SqlCommand
Dim SQLCmd4 As SqlCommand
Dim SQLCmd5 As SqlCommand
Dim SQLCmd_SID As SqlCommand
Dim SQLCmd_ARID As SqlCommand
Dim SQLCmd_ALID As SqlCommand
Dim SQLCmd_ARID2 As SqlCommand
Dim SQLCmd_ALID2 As SqlCommand
Dim SID
Dim ARID
Dim ALID
SQLcon.Open()
SQLCmd = New SqlCommand("INSERT INTO SONGS_DB (SONG_NAME, SONG_LENGTH) VALUES (@SONG_NAME, @SONG_LENGTH)", SQLcon)
SQLCmd.Parameters.AddWithValue("@SONG_NAME", Title)
SQLCmd.Parameters.AddWithValue("@SONG_LENGTH", Duration)
SQLCmd_SID = New SqlCommand("SELECT max(SONG_ID) FROM SONGS_DB", SQLcon)
If SID = "" Then
SID += 1
Else
SID = SQLCmd_SID.ExecuteScalar()
SID = Convert.ToInt32(SID)
End If
If Album IsNot "" Then
SQLCmd2 = New SqlCommand("INSERT INTO ALBUM_DB (NAME) VALUES (@NAME)", SQLcon)
SQLCmd2.Parameters.AddWithValue("@NAME", Album)
'SQLCmd2.Parameters.AddWithValue("@SONG_ID", SID)
End If
If Artist IsNot "" Then
SQLCmd3 = New SqlCommand("INSERT INTO ARTIST_DB (NAME) VALUES (@NAME)", SQLcon)
SQLCmd3.Parameters.AddWithValue("@NAME", Artist)
'SQLCmd3.Parameters.AddWithValue("@SONG_ID", SID)
End If
SQLCmd.ExecuteNonQuery()
If Album IsNot "" Then
SQLCmd2.ExecuteNonQuery()
End If
If Artist IsNot "" Then
SQLCmd3.ExecuteNonQuery()
End If
If Artist IsNot "" Then
SQLCmd_ARID = New SqlCommand("SELECT max(ARTIST_ID) FROM ARTIST_DB", SQLcon)
If ARID = "" Then
ARID += 1
Else
ARID = SQLCmd_ARID.ExecuteScalar()
ARID = Convert.ToInt32(ARID)
End If
SQLCmd_ARID2 = New SqlCommand("UPDATE SONG_DB SET ARTIST_ID = (@ARTIST_ID) WHERE SONG_ID = (@SONG_ID)", SQLcon)
SQLCmd_ARID2.Parameters.AddWithValue("@ARTIST_ID", ARID)
SQLCmd_ARID2.Parameters.AddWithValue("@SONG_ID", SID)
End If
If Album IsNot "" Then
SQLCmd_ALID = New SqlCommand("SELECT max(ALBUM_ID) FROM ALBUM_DB", SQLcon)
If ALID = "" Then
ALID += 1
Else
ALID = SQLCmd_ARID.ExecuteScalar()
ALID = Convert.ToInt32(ARID)
End If
SQLCmd_ALID2 = New SqlCommand("UPDATE SONG_DB SET ARTIST_ID = (@ARTIST_ID) WHERE SONG_ID = (@SONG_ID)", SQLcon)
SQLCmd_ALID2.Parameters.AddWithValue("@ARTIST_ID", ARID)
SQLCmd_ALID2.Parameters.AddWithValue("@SONG_ID", SID)
End If
If Artist IsNot "" Then
SQLCmd_ARID2.ExecuteNonQuery()
End If
If Album IsNot "" Then
SQLCmd_ALID2.ExecuteNonQuery()
End If
SQLcon.Close()
End If
任何想法我哪里出错了?预先感谢!
答案 0 :(得分:1)
If Album IsNot "" Then
SQLCmd2 = New SqlCommand("INSERT INTO ALBUM_DB (NAME) VALUES (@NAME)", SQLcon)
SQLCmd2.Parameters.AddWithValue("@NAME", Album)
'SQLCmd2.Parameters.AddWithValue("@SONG_ID", SID)
End If
If Artist IsNot "" Then
SQLCmd3 = New SqlCommand("INSERT INTO ARTIST_DB (NAME) VALUES (@NAME)", SQLcon)
SQLCmd3.Parameters.AddWithValue("@NAME", Artist)
'SQLCmd3.Parameters.AddWithValue("@SONG_ID", SID)
End If
此代码的一半用引号引起来。 替换为:
If Album IsNot "" Then
SQLCmd2 = New SqlCommand("INSERT INTO ALBUM_DB (NAME) VALUES (@NAME)", SQLcon)
SQLCmd2.Parameters.AddWithValue("@NAME", Album)
SQLCmd2.Parameters.AddWithValue("@SONG_ID", SID)
End If
If Artist IsNot "" Then
SQLCmd3 = New SqlCommand("INSERT INTO ARTIST_DB (NAME) VALUES (@NAME)", SQLcon)
SQLCmd3.Parameters.AddWithValue("@NAME", Artist)
SQLCmd3.Parameters.AddWithValue("@SONG_ID", SID)
End If
如注释中所述,以下行未向insert语句添加任何内容,因为该语句内没有@SONG_ID
参数。这也可能引起问题。
SQLCmd3.Parameters.AddWithValue("@SONG_ID", SID)
SQLCmd2.Parameters.AddWithValue("@SONG_ID", SID)