This is part of my code, which I insert data into two tables. Tables primary key is defined as auto increment:: song table PK is song_id. file table PK is File_id. I want to insert into file table the song_id as FK. How should I do it? Thanks
try
{
string MyConnection1 = "datasource=localhost;port=3306;username=root;password=123";
string Query1 = "insert into myproject.song " +
"(song_name, house_number, song_text) " +
"values " +
"('" + line1 + "', '" + paragraphs.Length +
"', '" + filetext + "');" + //Insert song name, song huose count and full song text
"insert into myproject.file " +
"(File_Location, Words_number, Lines_number, File_name, song_id) " +
"values " +
"('" + strfilename + "','" + words.Length + "','" +
totallineCnt + "', '" + fileNameOnly +
"', ?????????????;"; //Insert table file details
MySqlConnection myConn1 = new MySqlConnection(MyConnection1);
MySqlCommand MyCommand1 = new MySqlCommand(Query1, myConn1);
MySqlDataReader MyReader1;
myConn1.Open();
MyReader1 = MyCommand1.ExecuteReader();
while (MyReader1.Read())
{
}
myConn1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
MySQL provides the LAST_INSERT_ID()
function which returns the last value of an autoincrement column. To use it you would perform one INSERT
, SELECT
the function's value, and INSERT
the returned value into the second table.
You should also be using prepared statements instead of dynamic SQL for a plethora of reasons which I won't go into here.
I'm not terribly comfortable with C#, but I'll give it a shot:
string MyConnection1 = "datasource=localhost;port=3306;username=root;password=123";
string Query1 = "insert into myproject.song " +
"(song_name, house_number, song_text) " +
"values " +
"(@SongName, @HouseNum, @SongText)"; //Insert song name, song huose count and full song full text
string Query2 = "SELECT LAST_INSERT_ID()";
string Query3 = "insert into myproject.file " +
"(File_Location, Words_number, Lines_number, File_name, song_id) " +
"values " +
"(@FileLoc, @WordCount, @LineCount, @FileName, @SongId"; //Insert the table file details
try
{
MySqlConnection myConn1 = new MySqlConnection(MyConnection1);
myConn1.Open();
MySqlCommand Cmd1 = new MySqlCommand(Query1, myConn1);
Cmd1.Parameters.AddWithValue("@SongName", line1);
Cmd1.Parameters.AddWithValue("@HouseNum", paragraphs.Length);
Cmd1.Parameters.AddWithValue("@SongText", filetext);
Cmd1.ExecuteNonQuery();
MySqlCommand Cmd2 = new MySqlCommand(Query2, myConn1);
object result = Cmd2.ExecuteScalar();
int songId = Convert.ToInt32(result);
MySqlCommand Cmd3 = new MySqlCommand(Query3, myConn1);
Cmd3.Parameters.AddWithValue("@FileLoc", strfilename);
Cmd3.Parameters.AddWithValue("@WordCount", words.Length);
Cmd3.Parameters.AddWithValue("@LineCount", totallineCnt);
Cmd3.Parameters.AddWithValue("@FileName", fileNameOnly);
Cmd3.Parameters.AddWithValue("@SongId", songId);
Cmd3.ExecuteNonQuery();
myConn1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
That's probably riddled with syntax errors, but should give you enough of an outline to get started.
Also, you really should wrap the entire operation (two INSERT
s and a SELECT
) in a transaction, but I leave that as an exercise for the reader.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.