阅读器关闭时尝试调用 Read 无效,解决方法
阅读器关闭时尝试调用 Read 无效请问是怎么回事,如何解决。下面是代码
SqlDataReader odr=LogingUserName();
if(odr.read())
{
......
}
public SqlDataReader LogingUserName()
{
try
{
StrSql = "select * from [WIN_ADMINISTRATOR] where [WIN_ADMIN_USERNAME]=@StrName and [WIN_ADMIN_PASSWORD]=@StrPass";
SqlParameter Username = new SqlParameter("@StrName", SqlDbType.NVarChar, 16);
Username.Value = Convert.ToString(username);
SqlParameter Password = new SqlParameter("@StrPass", SqlDbType.NVarChar, 50);
Password.Value = Convert.ToString(password);
SqlParameter[] ArryPars = { Username, Password };
return ExecuteReader(connString, CommandType.Text, StrSql, ArryPars);
}
catch
{
throw;
}
}
下面这个是类里面的
public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
try
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, cmdParms);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
connection.Close();
throw;
}
}
------解决方案--------------------
------解决方案--------------------C# code
public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
DataReader reader = null;
try
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, cmdParms);
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return reader;
}
catch (Exception e) { throw e; }
finally
{ if (reader != null) { reader.Close(); } }
}
------解决方案--------------------
别用READER 你那样用要出问题
如果访问量大了 连接池就暴了。。。