Failing at Database db = DatabaseFactory.CreateDat

2019-03-01 05:51发布

问题:

Failing over around Database db = DatabaseFactory.CreateDatabase(); and I'm not sure why. I executed the stored proc with manual parameters declared and she worked just fine. Though, I'm not being given an error or anything to go off of, so I'm not sure whats wrong. I stick a break in the code on each line leading up to, and after the Database db = DatabaseFactory.CreateDatabase(); line and it stops there, after I tell the IDE to continue it skips that line and the function ends.

For the record, my references are solid as well. I've cannibalized this code from another project I've done, so I'm likely just missing something silly. Regardless, here's the code:

public class PullDebtor
{
    public static DataTable ClientNumber(string strClientID)
    {

        DataSet dsDebtor = new DataSet();
        dsDebtor.Tables.Add("Debtors");
        DbCommand dbCommand = null;

        try
        {

            Database db = DatabaseFactory.CreateDatabase();

            string sqlCommand = "sp_PullClientID";
            DbCommand dbCommand1 = db.GetSqlStringCommand(sqlCommand);
            dbCommand1.CommandType = CommandType.StoredProcedure;
            db.AddInParameter(dbCommand1, "@ClientID", DbType.String, strClientID);

            db.ExecuteNonQuery(dbCommand1);

            dsDebtor = db.ExecuteDataSet(dbCommand1);
        }
        catch
        {
            return dsDebtor.Tables[0];
        }
        finally
        {

        }
        return dsDebtor.Tables[0];

    }
}
}

回答1:

Have you edited your .config sections? You need somethig like:

<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

And you also need a config section pointing to the connection string you have defined in your connection strings section:

<dataConfiguration defaultDatabase="Connection String" />


回答2:

You might know why had you not ignored the exception. catch { ... } is rarely a good idea (it's effectively never a good idea but I try to avoid always/never when giving recommendations).

Modify your code to something like this and see if you still need help:

try
{
    Database db = DatabaseFactory.CreateDatabase();

    ...
}
catch(Exception ex)
{
    // this will dump to the output window in VS when running a Debug build.
    // Release logging will require something different
    System.Diagnostics.Debug.WriteLine(ex);

    ...
}