First, use the ExecuteNonQuery
to write operations. After execute command, you can read the parameter from Parameters
collection, since you have set the parameter as a output parameter, for sample:
command.Parameters["name"].Direction = System.Data.ParameterDirection.Output;
command.ExecuteNonQuery();
object name = command.Parameters["name"].Value;
If you want to know what Id
the identity column generated after the insert, you could use SCOPE_IDENTITY()
sql command, and use the ExecuteScalar()
to get the result of command, for sample:
int id;
using (SqlConnection connection = new SqlConnection(AppConstants.ConnectionString))
{
string sql = @"INSERT INTO custom_customer (customer_name)
VALUES (@name);
SELECT SCOPE_IDENTITY();"
using (SqlCommand command = new SqlCommand(sql))
{
command.Parameters.Add(new SqlParameter("name", customer));
connection.Open();
id = (int) command.ExecuteScalar();
connection.Close();
}
}