The Ado.net Fill method does not throw an error when starting a stored procedure that does not exist

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

        // Set Up Command 
        SqlDatabase db = new SqlDatabase(ConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString);
        SqlCommand command = db.GetStoredProcCommand(StoredProcName) as SqlCommand;
        command.Connection = db.CreateConnection() as SqlConnection;

        // Set Up Events for Logging
        command.StatementCompleted += new StatementCompletedEventHandler(command_StatementCompleted);
        command.Connection.FireInfoMessageEventOnUserErrors = true;
        command.Connection.InfoMessage += new SqlInfoMessageEventHandler(Connection_InfoMessage);

        // Add Parameters
        foreach (Parameter parameter in Parameters)
        {
            db.AddInParameter(command, 
                parameter.Name, 
                (System.Data.DbType)Enum.Parse(typeof(System.Data.DbType), parameter.Type), 
                parameter.Value);
        }

            // Use the Old Style fill to keep the connection Open througout the population
            // and manage the Statement Complete and InfoMessage events
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataSet ds = new DataSet();

            // Open Connection
            command.Connection.Open();

            // Populate
            da.Fill(ds);

            // Dispose of the adapter
            if (da != null)
            {
                da.Dispose();
            }

            // If you do not explicitly close the connection here, it will leak!  
            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)
    {
        // These are not really errors unless the Number >0
        // if Number = 0 that is a print message
        foreach (SqlError sql in e.Errors)
        {
            if (sql.Number == 0)
            {
                Logger.WriteInfo("Sql Message",sql.Message);
            }
            else
            {

                // Whatever this was it was an error 
                throw new DataException(String.Format("Message={0},Line={1},Number={2},State{3}", sql.Message, sql.LineNumber, sql.Number, sql.State));
            }
        }
    }

, , , .

            // Populate
            try
            {
                da.Fill(ds);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }

, , , ?

+3
1

, InfoMessageHandler . , , . .

, Visual Studio 2008, . Visual Studio 2010, 3.5, ID , . Visual Studio 2010 .

, , .

+5

Source: https://habr.com/ru/post/1740489/


All Articles