我正在使用 Microsoft Practice Enterprice 数据将 WCF 连接到数据库。请注意,并非总是抛出此异常。它发生在 while 语句中。然后,如果我刷新页面(重新提交登录详细信息,与此 WCF 服务通信的 MVC 应用程序),代码执行没有任何问题。有人可以确定为什么 IDataRader 在堆栈执行是在 using 语句中时关闭吗?我的代码是:
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;
}
}
数据库连接:
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;
}