現在のクエリはSQLインジェクションに対して非常に脆弱です。最良の方法は、を使用してパラメーター化されたクエリを使用することSQLCommand and its parameters
です。そして、私はINSERT INTO...SELECT
あなたのクエリで使用する方が良いと思います
(
"
INSERT INTO book (title, isbn, authorID)
SELECT '" + BookTitle.Text.ToString() + "' as title,
'" + BookIsbn.Text.ToString() + "' AS isbn,
id as authorID
FROM author
WHERE first_name = '" + BookAuthor.Text.ToString() + "'
"
)
を使用してADO.Net
string query = "INSERT INTO book (title, isbn, authorID)
SELECT @title as title,
@isbn AS isbn,
id as authorID
FROM author
WHERE first_name = @author";
using (MySqlConnection conn = new MySqlConnection("connectionstringHere"))
{
using (MySqlCommand comm = new MySqlCommand())
{
comm.Connection = conn;
comm.CommandType = CommandType.Text;
comm.CommandText = query;
comm.Parameters.AddWithValue("@title", BookTitle.Text.ToString());
comm.Parameters.AddWithValue("@isbn", BookIsbn.Text.ToString());
comm.Parameters.AddWithValue("@author", BookAuthor.Text.ToString());
try
{
conn.Open();
comm.ExecuteNonQuery;
}
catch (MySqlException ex)
{
// error here
}
finally
{
conn.Close();
}
}
}