Anda tidak boleh membagi kueri menjadi dua kueri. Anda harus memilih/mengatur hasilnya dalam kueri penyisipan.
Saya membuat contoh untuk Anda:
Metode pertama mengembalikan nilai tunggal dengan menggunakan select
private void Method1()
{
string sEmail = "[email protected]";
string passwordHash = "#[email protected]#[email protected]!#@[email protected]#!#@$!#@$!";
string salt = "????";
string sName = "John";
using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
try
{
sqlConnection.Open();
string insertStatement = "INSERT INTO [User] "
+ "(email, hash, salt, name) "
+ "VALUES (@email, @hash, @salt, @name)"
+ "SELECT SCOPE_IDENTITY()";
using (SqlCommand insertCommand = new SqlCommand(insertStatement, sqlConnection))
{
insertCommand.Parameters.Add("@email", SqlDbType.VarChar, 50).Value = sEmail;
insertCommand.Parameters.Add("@hash", SqlDbType.VarChar, 50).Value = passwordHash;
insertCommand.Parameters.Add("@salt", SqlDbType.VarChar, 50).Value = salt;
insertCommand.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = sName;
int userId = Convert.ToInt32(insertCommand.ExecuteScalar());
Trace.WriteLine("User created with id: " + userId);
}
}
catch (SqlException ex)
{
Trace.WriteLine(ex.Message);
//lblMessage.Text = ex.Message;
}
}
Metode kedua adalah mendefinisikan parameter keluaran, dengan cara ini Anda dapat mengembalikan beberapa nilai.
private void Method2()
{
string sEmail = "[email protected]";
string passwordHash = "#[email protected]#[email protected]!#@[email protected]#!#@$!#@$!";
string salt = "????";
string sName = "John";
using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
try
{
sqlConnection.Open();
string insertStatement = "INSERT INTO [User] "
+ "(email, hash, salt, name) "
+ "VALUES (@email, @hash, @salt, @name)"
+ "SET @user_id = SCOPE_IDENTITY()";
using (SqlCommand insertCommand = new SqlCommand(insertStatement, sqlConnection))
{
insertCommand.Parameters.Add("@email", SqlDbType.VarChar, 50).Value = sEmail;
insertCommand.Parameters.Add("@hash", SqlDbType.VarChar, 50).Value = passwordHash;
insertCommand.Parameters.Add("@salt", SqlDbType.VarChar, 50).Value = salt;
insertCommand.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = sName;
insertCommand.Parameters.Add("@user_id", SqlDbType.Int).Direction = ParameterDirection.Output;
insertCommand.ExecuteNonQuery();
int userId = Convert.ToInt32(insertCommand.Parameters["@user_id"].Value);
Trace.WriteLine("User created with id: " + userId);
}
}
catch (SqlException ex)
{
Trace.WriteLine(ex.Message);
//lblMessage.Text = ex.Message;
}
}
Yang terbaik yang dapat Anda lakukan adalah, jika kuerinya statis, menempatkan kueri ini dalam prosedur tersimpan. Ini akan mempercepat kueri.