我想知道在.Net 中开发 CRUD 操作(特别是当您使用数据库作为数据源时)时,在 try-catch 块的 catch 部分中是否有任何自信的方法?
好吧,您对以下几行有何看法?
public int Insert(string name, Int32 employeeID, string createDate)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = this._ConnectionString;
try
{
SqlCommand command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "UnitInsert";
if (connection.State != ConnectionState.Open)
connection.Open();
SqlCommandBuilder.DeriveParameters(command);
command.Parameters["@Name"].Value = name;
command.Parameters["@EmployeeID"].Value = employeeID;
command.Parameters["@CreateDate"].Value = createDate;
int i = command.ExecuteNonQuery();
command.Dispose();
return i;
}
catch
{
**// how do you "catch" any possible error here?**
return 0;
//
}
finally
{
connection.Close();
connection.Dispose();
connection = null;
}
}