我的方法中出现空引用异常。如何解决?
Null reference exception in my method. How to fix it?
这是我的代码。我认为问题出在我的 declare @EmployeeId
.
string strCommand = "insert into Employee (FirstName, LastName, Birthday, Inn)" +
" values (@FirstName, @LastName, @Birthday, @Inn)" +
" declare @EmployeeId int = @@identity";
SqlCommand command = new SqlCommand(strCommand, connection);
command.Parameters.AddWithValue("@FirstName", employee.FirstName);
command.Parameters.AddWithValue("@LastName", employee.LastName);
command.Parameters.AddWithValue("@Birthday", employee.Birthday);
command.Parameters.AddWithValue("@Inn", employee.INN);
SqlParameter parameter = new SqlParameter("@EmployeeId", System.Data.SqlDbType.Int);
parameter.Direction = System.Data.ParameterDirection.Output;
//command.Parameters.Add(parameter); //???
try
{
connection.Open();
command.ExecuteNonQuery();
employee.EmployeeId = (int)parameter.Value; // Here I get the exception
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
// new Exception("Employee Not Insertes");
}
finally
{
connection.Close();
}
return employee;
这只是一种根本不起作用的方法。
对不起我的英语。
我会这样做:
string sql = "
insert into Employee(FirstName,LastName,Birthday,Inn)
output inserted.Id -- or inserted.employeeid, if that's the column name
values(@FirstName,@LastName,@Birthday,@Inn);";
// You should create a new connection object for most queries. Really.
// Read this for more info why: https://softwareengineering.stackexchange.com/q/142065/8057
// You should also wrap that connection in a using block
using (var connection = new SqlConnection("connection string here"))
using (var command = new SqlCommand(sql, connection))
{
//You should avoid AddWithValue(). In certain situation it can cause a severe performance penalty
//Instead, you should use an explicit type and length
command.Parameters.Add("@FirstName", SqlDbType.NVarChar, 15).Value = employee.FirstName;
command.Parameters.Add("@LastName", SqlDbType.NVarChar, 20).Value = employee.LastName;
command.Parameters.Add("@Birthday", SqlDbType.DateTime).Value = employee.Birthday);
command.Parameters.Add("@Inn", SqlDbType.Int).Value = employee.INN; //not sure what INN represents. Some Number?
connection.Open();
employee.EmployeeId = (int)command.ExecuteScalar();
}
return employee;
请注意,这允许我完全删除带有异常的行。就此而言,它允许我在这里完全删除异常处理。相反,它可以移动到调用代码 - 它所属的位置 - 因为不再需要 finally
块来关闭连接。
这是我的代码。我认为问题出在我的 declare @EmployeeId
.
string strCommand = "insert into Employee (FirstName, LastName, Birthday, Inn)" +
" values (@FirstName, @LastName, @Birthday, @Inn)" +
" declare @EmployeeId int = @@identity";
SqlCommand command = new SqlCommand(strCommand, connection);
command.Parameters.AddWithValue("@FirstName", employee.FirstName);
command.Parameters.AddWithValue("@LastName", employee.LastName);
command.Parameters.AddWithValue("@Birthday", employee.Birthday);
command.Parameters.AddWithValue("@Inn", employee.INN);
SqlParameter parameter = new SqlParameter("@EmployeeId", System.Data.SqlDbType.Int);
parameter.Direction = System.Data.ParameterDirection.Output;
//command.Parameters.Add(parameter); //???
try
{
connection.Open();
command.ExecuteNonQuery();
employee.EmployeeId = (int)parameter.Value; // Here I get the exception
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
// new Exception("Employee Not Insertes");
}
finally
{
connection.Close();
}
return employee;
这只是一种根本不起作用的方法。 对不起我的英语。
我会这样做:
string sql = "
insert into Employee(FirstName,LastName,Birthday,Inn)
output inserted.Id -- or inserted.employeeid, if that's the column name
values(@FirstName,@LastName,@Birthday,@Inn);";
// You should create a new connection object for most queries. Really.
// Read this for more info why: https://softwareengineering.stackexchange.com/q/142065/8057
// You should also wrap that connection in a using block
using (var connection = new SqlConnection("connection string here"))
using (var command = new SqlCommand(sql, connection))
{
//You should avoid AddWithValue(). In certain situation it can cause a severe performance penalty
//Instead, you should use an explicit type and length
command.Parameters.Add("@FirstName", SqlDbType.NVarChar, 15).Value = employee.FirstName;
command.Parameters.Add("@LastName", SqlDbType.NVarChar, 20).Value = employee.LastName;
command.Parameters.Add("@Birthday", SqlDbType.DateTime).Value = employee.Birthday);
command.Parameters.Add("@Inn", SqlDbType.Int).Value = employee.INN; //not sure what INN represents. Some Number?
connection.Open();
employee.EmployeeId = (int)command.ExecuteScalar();
}
return employee;
请注意,这允许我完全删除带有异常的行。就此而言,它允许我在这里完全删除异常处理。相反,它可以移动到调用代码 - 它所属的位置 - 因为不再需要 finally
块来关闭连接。