Du bør ikke opdele forespørgslen i to forespørgsler. Du bør vælge/indstille resultatet i indsættelsesforespørgslen.
Jeg lavede et eksempel til dig:
Den første metode er at returnere en enkelt værdi ved brug af 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;
}
}
Den anden metode er at definere en output-parameter, på denne måde kan du returnere flere værdier.
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;
}
}
Det bedste du kan gøre er, hvis forespørgslerne er statiske, at placere disse forespørgsler i lagrede procedurer. Dette vil fremskynde forespørgslerne.