I use a combination of the Enterprise library and the original Fill method for ADO. This is due to the fact that I need to open and close the connection with the command myself, as I capture the Info Message event
Here is my code so far
SqlDatabase db = new SqlDatabase(ConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString);
SqlCommand command = db.GetStoredProcCommand(StoredProcName) as SqlCommand;
command.Connection = db.CreateConnection() as SqlConnection;
command.StatementCompleted += new StatementCompletedEventHandler(command_StatementCompleted);
command.Connection.FireInfoMessageEventOnUserErrors = true;
command.Connection.InfoMessage += new SqlInfoMessageEventHandler(Connection_InfoMessage);
foreach (Parameter parameter in Parameters)
{
db.AddInParameter(command,
parameter.Name,
(System.Data.DbType)Enum.Parse(typeof(System.Data.DbType), parameter.Type),
parameter.Value);
}
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
command.Connection.Open();
da.Fill(ds);
if (da != null)
{
da.Dispose();
}
if (command.Connection.State == ConnectionState.Open)
{
command.Connection.Close();
}
...
Now, if I go to the variable StoredProcName = "ThisProcDoesNotExists"
And run this piece of code. CreateCommand or da.Fill via error message. Why is this. The only way I can say that it did not start was that it returns a dataset with 0 tables. But when investigating the error, it is not visible that the procedure does not exist.
command.Connection.FireInfoMessageEventOnUserErrors = true;
, InfoMessage
BOL
FireInfoMessageEventOnUserErrors true, , , InfoMessage. . FireInfoMessageEventOnUserErrors false, InfoMessage .
, Sql . false . , true, , Error
, , true
void Connection_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
foreach (SqlError sql in e.Errors)
{
if (sql.Number == 0)
{
Logger.WriteInfo("Sql Message",sql.Message);
}
else
{
throw new DataException(String.Format("Message={0},Line={1},Number={2},State{3}", sql.Message, sql.LineNumber, sql.Number, sql.State));
}
}
}
, , , .
try
{
da.Fill(ds);
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
, , , ?