- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用 C# 和 .NET 4.0 的 ASP.NET 网络表单站点。
我创建了一个类,用于将信息从 SQL 表加载到对象中,一段时间以来一直运行良好。最近,一些特定的记录使我用来填充此类的 SqlDataReader
崩溃,并出现数据库超时错误。我找不到任何理由让这些记录让读者崩溃。
我已将崩溃隔离到 [Address] int
类型的数据字段,当从查询中排除该数据字段时,读取器可以正常工作。我检查了数据库,存储的值没有异常,将它们更改为 0、null 或其他工作数据,仍然会导致超时错误。如果我使用 ExecuteScalar()
调用字段,数据会正确填充而不会出错。
是什么导致了这种行为?
错误内容如下:
Server Error in '/' Application.
A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - The specified network name is no longer available.)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - The specified network name is no longer available.)
来源方法
public void Populate(Guid UserId)
{
DAL db = new DAL();
using (SqlConnection con = db.GetConnection())
{
SqlCommand RowCount = new SqlCommand("SELECT COUNT([UserId]) FROM aspnet_Membership WHERE [UserId]=@UserId", con);
RowCount.Parameters.Add(new SqlParameter("@UserId", SqlDbType.UniqueIdentifier));
RowCount.Parameters["@UserId"].Value = UserId;
SqlDataReader rdr = null;
string sqlQuery = "SELECT [ApplicationId],[UserId],[Password],[PasswordFormat],[PasswordSalt],[MobilePIN],"+
"[Email],[LoweredEmail],[PasswordQuestion],[PasswordAnswer],[IsApproved],[IsLockedOut],[CreateDate],[LastLoginDate],[LastPasswordChangedDate],[LastLockOutDate]," +
"[FailedPasswordAttemptCount],[FailedPasswordAttemptWindowStart],[FailedPasswordAnswerAttemptCount],[FailedPasswordAnswerAttemptWindowStart],[Comment]," +
"[FirstName],[LastName],[Address] FROM aspnet_Membership WHERE [UserId]=@UserId";
SqlCommand sqlCommand = new SqlCommand(sqlQuery, con);
sqlCommand.Parameters.Add(new SqlParameter("@UserId", SqlDbType.UniqueIdentifier));
sqlCommand.Parameters["@UserId"].Value = UserId;
try
{
con.Open();
int Rows = (int)RowCount.ExecuteScalar();
if (Rows > 0)
{
rdr = sqlCommand.ExecuteReader();
while (rdr.Read())
{
this.ApplicationId = (Guid)rdr["ApplicationID"];
this.UserId = (Guid)rdr["UserId"];
this.Password = common.Coalesce(rdr["Password"], "");
this.PasswordFormat = common.Coalesce(rdr["PasswordFormat"], 0);
this.PasswordSalt = common.Coalesce(rdr["PasswordSalt"], "");
this.MobilePIN = common.Coalesce(rdr["MobilePIN"], "");
this.Email = common.Coalesce(rdr["Email"], "");
this.LoweredEmail = common.Coalesce(rdr["LoweredEmail"], "");
this.PasswordQuestion = common.Coalesce(rdr["PasswordQuestion"], "");
this.PasswordAnswer = common.Coalesce(rdr["PasswordAnswer"], "");
this.IsApproved = (bool)rdr["IsApproved"];
this.IsLockedOut = (bool)rdr["IsLockedOut"];
this.CreateDate = common.Coalesce(rdr["CreateDate"], DateTime.Now);
this.LastLoginDate = common.Coalesce(rdr["LastLoginDate"], DateTime.Now);
this.LastPasswordChangedDate = common.Coalesce(rdr["LastPasswordChangedDate"], DateTime.Now);
this.LastLockOutDate = common.Coalesce(rdr["LastLockOutDate"], DateTime.Now);
this.FailedPasswordAttemptCount = common.Coalesce(rdr["FailedPasswordAttemptCount"], 0);
this.FailedPasswordAttemptWindowStart = common.Coalesce(rdr["FailedPasswordAttemptWindowStart"], DateTime.Now);
this.FailedPasswordAnswerAttemptCount = common.Coalesce(rdr["FailedPasswordAnswerAttemptCount"], 0);
this.FailedPasswordAnswerAttemptWindowStart = common.Coalesce(rdr["FailedPasswordAnswerAttemptWindowStart"], DateTime.Now);
this.Comment = common.Coalesce(rdr["Comment"], "");
this.FirstName = common.Coalesce(rdr["FirstName"], "");
this.LastName = common.Coalesce(rdr["LastName"], "");
this.Address = common.Coalesce(rdr["Address"], 0);
}
rdr.Close();
}
con.Close();
}
catch (Exception ex) { if (con != null) { con.Close(); } throw ex; }
finally { if (con != null) { con.Close(); } }
}
}
修改后的方法不报错
public void Populate(Guid UserId)
{
DAL db = new DAL();
using (SqlConnection con = db.GetConnection())
{
SqlCommand RowCount = new SqlCommand("SELECT COUNT([UserId]) FROM aspnet_Membership WHERE [UserId]=@UserId", con);
RowCount.Parameters.Add(new SqlParameter("@UserId", SqlDbType.UniqueIdentifier));
RowCount.Parameters["@UserId"].Value = UserId;
SqlDataReader rdr = null;
string sqlQuery = "SELECT [ApplicationId],[UserId],[Password],[PasswordFormat],[PasswordSalt],[MobilePIN],"+
"[Email],[LoweredEmail],[PasswordQuestion],[PasswordAnswer],[IsApproved],[IsLockedOut],[CreateDate],[LastLoginDate],[LastPasswordChangedDate],[LastLockOutDate]," +
"[FailedPasswordAttemptCount],[FailedPasswordAttemptWindowStart],[FailedPasswordAnswerAttemptCount],[FailedPasswordAnswerAttemptWindowStart],[Comment]," +
"[FirstName],[LastName] FROM aspnet_Membership WHERE [UserId]=@UserId";
SqlCommand sqlCommand = new SqlCommand(sqlQuery, con);
sqlCommand.Parameters.Add(new SqlParameter("@UserId", SqlDbType.UniqueIdentifier));
sqlCommand.Parameters["@UserId"].Value = UserId;
try
{
con.Open();
int Rows = (int)RowCount.ExecuteScalar();
if (Rows > 0)
{
rdr = sqlCommand.ExecuteReader();
while (rdr.Read())
{
this.ApplicationId = (Guid)rdr["ApplicationID"];
this.UserId = (Guid)rdr["UserId"];
this.Password = common.Coalesce(rdr["Password"], "");
this.PasswordFormat = common.Coalesce(rdr["PasswordFormat"], 0);
this.PasswordSalt = common.Coalesce(rdr["PasswordSalt"], "");
this.MobilePIN = common.Coalesce(rdr["MobilePIN"], "");
this.Email = common.Coalesce(rdr["Email"], "");
this.LoweredEmail = common.Coalesce(rdr["LoweredEmail"], "");
this.PasswordQuestion = common.Coalesce(rdr["PasswordQuestion"], "");
this.PasswordAnswer = common.Coalesce(rdr["PasswordAnswer"], "");
this.IsApproved = (bool)rdr["IsApproved"];
this.IsLockedOut = (bool)rdr["IsLockedOut"];
this.CreateDate = common.Coalesce(rdr["CreateDate"], DateTime.Now);
this.LastLoginDate = common.Coalesce(rdr["LastLoginDate"], DateTime.Now);
this.LastPasswordChangedDate = common.Coalesce(rdr["LastPasswordChangedDate"], DateTime.Now);
this.LastLockOutDate = common.Coalesce(rdr["LastLockOutDate"], DateTime.Now);
this.FailedPasswordAttemptCount = common.Coalesce(rdr["FailedPasswordAttemptCount"], 0);
this.FailedPasswordAttemptWindowStart = common.Coalesce(rdr["FailedPasswordAttemptWindowStart"], DateTime.Now);
this.FailedPasswordAnswerAttemptCount = common.Coalesce(rdr["FailedPasswordAnswerAttemptCount"], 0);
this.FailedPasswordAnswerAttemptWindowStart = common.Coalesce(rdr["FailedPasswordAnswerAttemptWindowStart"], DateTime.Now);
this.Comment = common.Coalesce(rdr["Comment"], "");
this.FirstName = common.Coalesce(rdr["FirstName"], "");
this.LastName = common.Coalesce(rdr["LastName"], "");
}
rdr.Close();
string sqlQuery2 = "SELECT [Address] FROM aspnet_Membership WHERE [UserId]=@UserId";
SqlCommand sqlCommand2 = new SqlCommand(sqlQuery2, con);
sqlCommand2.Parameters.Add(new SqlParameter("@UserId", SqlDbType.UniqueIdentifier));
sqlCommand2.Parameters["@UserId"].Value = UserId;
this.Address = common.Coalesce(sqlCommand2.ExecuteScalar(), 0);
}
con.Close();
}
catch (Exception ex) { if (con != null) { con.Close(); } throw ex; }
finally { if (con != null) { con.Close(); } }
}
}
最佳答案
您应该向我们展示比这更多的代码,我认为这只是一个间接错误。
通常打开已经打开的连接或关闭已经关闭的连接会导致无效操作异常
,这就是您在这里所做的。
try{
con.Open();
//do something
con.Close(); //will be closed when no error was raised
}catch (Exception ex){
if (con != null){
// this will close if the error was raised in "do something"
con.Close();
}
throw ex; // you better thow the exception by throw (instead of throw ex) to keep the stacktrace
}finally {
if (con != null) {
// this will definitely cause an Invalid Operation Exception since the connection was already closed
con.Close();
}
}
您应该改为使用 using-statement隐含地关闭和处理连接。如果你想手动关闭它,你还应该检查它的 ConnectionState :
if (con != null && con.State != System.Data.ConnectionState.Closed){con.Close();}
使用 using 语句规避此问题的示例:
try{
using (var con = new System.Data.SqlClient.SqlConnection(conString)) {
using(var cmd = new System.Data.SqlClient.SqlCommand(command, con)){
con.open();
var reader = cmd.ExecuteReader();
while (reader.Read()) {
//do something
}
}
}//will automatically close connection
}
catch (Exception ex) {
//log exception and/or throw
throw;
}
关于c# - 当 ExecuteScalar 工作正常时,为什么 ExecuteReader 会崩溃读取某些记录的特定数据字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9168219/
我正在尝试返回 DbSet哪里Survey定义为 public class Survey { public int Id { get; set; } [StringLength(100
我使用库“Dapper”连接 MS Sql。 当我调用存储过程时,它显示错误信息 can not find stored procedure. 但是存储过程它已经存在于数据库中。我该如何解决这个问题?
给我这个错误连接必须有效且打开我不知道如何解决它。 private void loginUtenti(string user, string psw) { create
我试图在一个 int 变量中获取 sql 查询的结果,但我收到了对象空引用错误。有人可以指导我吗。 oconn = new SqlConnection(oSession.CONNECTION_STR
我有这个模块 strSQL = "Insert Into TblInsurancePersonel (CompId,PersonelNo,FirstName,LastName) "; st
下面的代码有点问题。 NextIPID() 方法应该简单地查看 SQL Server 中的一个表,该表返回字段 IPID 的最大值,然后将其加 1,以获得序列中的下一个数字。 public int N
我正在使用以下 SQL 查询和 ExecuteScalar() 方法从 Oracle 数据库中获取数据: sql = "select username from usermst where useri
我正在尝试使用 ExecuteScalar() 从数据库中返回一个整数。但是,当我在数据库本身上运行查询时,我得到了正确的答案,并且 c# 始终给我一个 0(Null)。我知道它返回一个空值,因为在我
ExecuteScalar会自动关闭连接吗? 最佳答案 不,使用 ExecuteScalar() 时需要显式打开和关闭连接。 关于ADO.NET:ExecuteScalar 是否自动关闭连接?,我们在
我正在使用 SQL Server 2005 和 C#。我想在向数据库中插入记录时获取主键值。 为此,我有一个 C# 方法,我可以在其中插入和更新记录。我正在使用 ExecuteScalar 方法进行插
我正在使用 ExecuteScalar 获取一列: cmd.commandtext = "select rodeuser from customer_db_map"; string rode
ExecuteScalar() 有时返回空对象 - 而不是 null- 尽管记录存在。当我用 quickwatch 分析这个对象时,我看到 object.GetType() 等于 DbNull。我可以
我在同一个 ExecuteScalar 命令中同时检查 2 条记录时遇到问题。 我想在本地数据库中不同列的同一行中一起搜索2条记录,我尝试像下面的代码一样进行操作,但只有在1个条件下执行它才可以,但我
关键是我有这段代码 var ojv = xmd.ExecuteScalar().ToString(); if (ojv != null) { C
当我运行以下代码时: query = "select count(*) from table where name = '?name'"; MySqlConnection connection =
我有一个使用事务的 sql 查询,它使用 return 来指示失败或成功,如下所示: BEGIN DECLARE @intErrorCode INT; DECLARE @emailAu
下面是我的程序,用于测量 ExecuteScalar 进行多次迭代所花费的时间。 static void Main(string[] args) { string sqlConnectionS
我正在用可视化 C# 编写一个程序,我在其中提取数据并将其放入 dataGridView 中。这一切都很好,但如果我之后尝试提取更多数据(我想将数据放入文本框),它会出现错误。 这是我提取数据并将其放
我试过各种方法,但似乎都行不通。由于结果为空,例程一直返回 0。 这是我的代码: string strSql = "SELECT [ID] FROM [PurchaseOrder].[dbo].[FM
我正在使用 Xamarin 中的 MySQL 库连接到我的数据库。我正在调用 ExecuteScalar() 命令来检查我的数据库中是否存在用户。 我将 ExecuteScalar() 的返回转换为
我是一名优秀的程序员,十分优秀!