读取器关闭异常时无效尝试调用MetaData

时间:2013-09-15 10:46:50

标签: c# wcf exception idatareader

我使用Microsoft Practice Enterprice数据将WCF连接到数据库。请注意,不会始终抛出此异常。它发生在while语句中。然后,如果我刷新页面(重新提交登录详细信息,与此WCF服务通信的MVC应用程序),代码执行没有任何问题。当堆栈执行在using语句中时,有人可以确定IDataRader关闭的原因吗? 我的代码是:

public class LoginService : ILoginService
{
    public Attendee CheckLoginCredentials(string username, string password, int databaseId)
    {
        Database db = DataBaseConnection.GetDatabaseObject(databaseId);
        Attendee attendee = new Attendee();
        try
        {
            DbCommand dbCommandWrapper = db.GetStoredProcCommand("Check_login_WCF");
            db.AddInParameter(dbCommandWrapper, "@Username", DbType.String, username);
            db.AddInParameter(dbCommandWrapper, "@Password", DbType.String, password);

            using (IDataReader dataReader = db.ExecuteReader(dbCommandWrapper))
            {
                while (dataReader.Read())
                {
                    if (!dataReader.IsDBNull(dataReader.GetOrdinal("PersonId")))
                        attendee.PersonId = dataReader.GetGuid(dataReader.GetOrdinal("PersonId"));


                    if (!dataReader.IsDBNull(dataReader.GetOrdinal("UserName")))
                        attendee.UserName = dataReader.GetString(dataReader.GetOrdinal("UserName"));


                    if (!dataReader.IsDBNull(dataReader.GetOrdinal("IsValid")))
                        attendee.ValidCredentials = dataReader.GetBoolean(dataReader.GetOrdinal("ValidCredentials"));
                }
            }


        }
        catch (Exception ex)
        {
            return null;
        }

        return attendee;
    }
}

的DatabaseConnection:

public static Database GetDatabaseObject(int databaseId)
{
    string ConnectionString = string.Empty;
    ConnectionString = EventDatabases.GetConnectionString(databaseId);

    Database mydb = new Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase(ConnectionString);
    return mydb;
}

0 个答案:

没有答案