读取数据类是
string sql = "select top " + tmpPagesize + " " + tmpField + " from " + tableName + " where " + tableId + " not in(select top " + top + " " + tableId + " from " + tableName + " where " + where + " " + tmpOrder + ") and " + where + " " + tmpOrder + "";
using (SqlDataReader reader = DBHELPER.GetReader(sql))
{
while (reader.Read())
{
list.Add(getListByRows(reader));
}
reader.Close();
}
return list; public static SqlDataReader GetReader(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
string sql = "select top " + tmpPagesize + " " + tmpField + " from " + tableName + " where " + tableId + " not in(select top " + top + " " + tableId + " from " + tableName + " where " + where + " " + tmpOrder + ") and " + where + " " + tmpOrder + "";
using (SqlDataReader reader = DBHELPER.GetReader(sql))
{
while (reader.Read())
{
list.Add(getListByRows(reader));
}
reader.Close();
}
return list; public static SqlDataReader GetReader(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
string sql = "select top " + tmpPagesize + " " + tmpField + " from " + tableName + " where " + tableId + " not in(select top " + top + " " + tableId + " from " + tableName + " where " + where + " " + tmpOrder + ") and " + where + " " + tmpOrder + "";
using (SqlDataReader reader = DBHELPER.GetReader(sql))
{
while (reader.Read())
{
list.Add(getListByRows(reader));
}
reader.Close(); }
return list;
红色的部分去掉,
那会发生reader未关闭的错误吗
我是SERVER2008 英文操作系统 SQL08
reader.close() 不算关闭吗
致命的内部连接错误。 using (SqlDataReader reader = DBHELPER.GetReader(sql))
{
while (reader.Read())
{
//怀疑是getListByRows(reader)方法内部提前关闭了reader
list.Add(getListByRows(reader));
}
reader.Close(); //这句话没问题,此时处理就不会等到以后交给托管来清理。
}
return list;
string sql = "select top " + tmpPagesize + " " + tmpField + " from " + tableName + " where " + tableId + " not in(select top " + top + " " + tableId + " from " + tableName + " where " + where + " " + tmpOrder + ") and " + where + " " + tmpOrder + "";
using (SqlDataReader reader = DBHELPER.GetReader(sql))
{
while (reader.Read())
{
//省略
list.Add(getListByRows(reader));
}
reader.Close();
}
return list;DBHELPER
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
public static SqlDataReader GetReader(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
public static SqlConnection Connection
可能问题就出现在这里。很难想象1000个人同时使用这个连接是个什么样子。
所以我建议楼主对每个数据库请求都新建一个SqlConnection,即采用如下方式:
using(SqlConnection conn=new SqlConnection(DefaultConnectionString))
{
//执行你要的操作
}
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}改成
public static SqlDataReader GetReader(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
应该是静态造成的