说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Exception: ExecuteReader 要求已打开且可用的连接。连接的当前状态为正在连接。
源错误:
行 316: catch (Exception e)行 317: {行 318: throw new Exception(e.Message);行 319:行 320: }
|
源文件: f:\jjhsz\App_Code\DB.cs 行: 318
堆栈跟踪:
[Exception: ExecuteReader 要求已打开且可用的连接。连接的当前状态为正在连接。] DB.bindrepeater(String sql, Repeater rp) in f:\jjhsz\App_Code\DB.cs:318 ShowList.binddr() in f:\jjhsz\Show_article.aspx.cs:43 ShowList.Page_Load(Object sender, EventArgs e) in f:\jjhsz\Show_article.aspx.cs:37 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627 |
根本原因在于你用了静态的链接,public static OleDbConnection Conn;
你想啊,ASP代码执行起来肯定是多线程的,你在访问,别人也在访问,当两个请求都执行到你的代码的时候,如果一个请求正在用这个Conn执行命令,另一个却把这个链接给关了,你说不就报错了嘛.
看到很多SqlHelper里面,很多人自作聪明的把Conn给提出来,搞成静态的,以为这样就不用每次new Conn了,殊不知到这样就会出问题了啊...
解决方案也很简单,你只需要干掉原来静态的Conn每次执行的时候都new一个链接对象,相信我,new 一个链接对象那点内存开销不算事儿.
连接没有关闭
1,access数据库你有没有手动打开表在查看?
2,access数据库你是不是已经打开连接着没有关闭?
3,程序中是不是只建立了连接没有open啊?
1、2、3问题都可以排除!
public class DB
{
public static OleDbConnection Conn;
public static string ConnString;//连接字符串
public DB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static OleDbConnection Getconn()
{
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ConnectionString"].ToString());
Conn = new OleDbConnection(ConnString);
//if (Conn.State.Equals(ConnectionState.Closed))
//{
// Conn.Open();
//}
if (Conn == null)
{
Conn = new OleDbConnection(ConnString);
Conn.Open();
}
else if (Conn.State == System.Data.ConnectionState.Closed)
{
Conn.Open();
}
else if (Conn.State == System.Data.ConnectionState.Broken)
{
Conn.Close();
Conn.Open();
}
return Conn;
}
//=================================================
//功能描述:关闭数据库
//时间:2010.11.10
//=================================================
private static void closeConnection()
{
OleDbConnection conn = DB.Getconn();
OleDbCommand cmd = new OleDbCommand();
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
cmd.Dispose();
}
}
//=================================================
//功能描述:执行SQL语句
//输入参数:sql,查询的SQL语句
//时间:2010.11.10
//=================================================
public static void execnonsql(string sql)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
OleDbCommand com = new OleDbCommand(sql, conn);
com.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:获取DATASET
//输入参数:sql,查询的SQL语句
//返回值:DataSet
//时间:2010.11.10
//=================================================
public static DataSet getdataset(string sql)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
OleDbDataAdapter adp = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
adp.Fill(ds, "ds");
return ds;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:获取DATASET1
//输入参数:sql,查询的SQL语句
//返回值:DataSet
//时间:2010.11.10
//=================================================
public static DataSet select(string sql, string tablename)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
OleDbDataAdapter adp = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
adp.Fill(ds, tablename);
return ds;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:获取某个字段数据
//输入参数:sql,查询的SQL语句
//返回值:hang
//时间:2010.11.10
//=================================================
public static string FindString(string sql)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
OleDbCommand com = new OleDbCommand(sql, conn);
string hang = Convert.ToString(com.ExecuteScalar());
return hang;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:对DATAGRIG进行数据绑定,无排序
//输入参数:sql,查询的SQL语句;dg,需要绑定的DATAGRID控件
//返回值:无
//时间:2010.11.10
//=================================================
public static void binddatagrid(string sql, DataGrid dg)
{
try
{
DataSet ds = getdataset(sql);
dg.DataSource = ds.Tables[0].DefaultView;
dg.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:对DropDownList进行数据绑定,无排序
//输入参数:sql,查询的SQL语句;dg,需要绑定的DATAGRID控件
//返回值:无
//时间:2010.11.10
//=================================================
public static void bindDropDownList(string sql, DropDownList dl, string class_name, string id)
{
try
{
DataSet ds = getdataset(sql);
dl.DataSource = ds.Tables[0].DefaultView;
dl.DataTextField = class_name;
dl.DataValueField = id;
dl.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:对RadioButtonList进行数据绑定,无排序
//输入参数:sql,查询的SQL语句;dg,需要绑定的DATAGRID控件
//返回值:无
//时间:2010.11.10
//=================================================
public static void bindRadioButtonList(string sql, RadioButtonList rl, string class_name, string id)
{
try
{
DataSet ds = getdataset(sql);
rl.DataSource = ds.Tables[0].DefaultView;
rl.DataTextField = class_name;
rl.DataValueField = id;
rl.SelectedIndex = 0;
rl.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:对GridView进行数据绑定,无排序
//输入参数:sql,查询的SQL语句;dg,需要绑定的DATAGRID控件
//返回值:无
//时间:2010.11.10
//=================================================
public static void bindGridView(string sql, GridView dg)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
DataSet ds = getdataset(sql);
dg.DataSource = ds.Tables[0].DefaultView;
dg.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:对datalist进行数据绑定,无排序
//输入参数:sql,查询的SQL语句;dl,需要绑定的datalist控件
//返回值:无
//时间:2010.11.10
//=================================================
public static void binddatalist(string sql, DataList dl)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
DataSet ds = getdataset(sql);
dl.DataSource = ds.Tables[0].DefaultView;
dl.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:对repeater进行数据绑定,无排序
//输入参数:sql,查询的SQL语句;dl,需要绑定的repeater控件
//返回值:无
//时间:2010.11.10
//=================================================
public static void bindrepeater(string sql, Repeater rp)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
DataSet ds = getdataset(sql);
rp.DataSource = ds.Tables[0].DefaultView;
rp.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:对listbox进行数据绑定
//输入参数:sql,查询的SQL语句;listb,需要绑定的listbox控件
//返回值:无
//时间:2010.11.10
//=================================================
public static void bindlistbox(string sql, ListBox listb, string class_name, string id)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
DataSet ds = getdataset(sql);
listb.DataSource = ds.Tables[0].DefaultView;
listb.DataTextField = class_name;
listb.DataValueField = id;
listb.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
/// <summary>
/// 返回 HTML 字符串的编码结果
/// </summary>
/// <param name="str">字符串</param>
/// <returns>编码结果</returns>
public static string HtmlEncode(string str)
{
return HttpUtility.HtmlEncode(str);
}
/// <summary>
/// 返回 HTML 字符串的解码结果
/// </summary>
/// <param name="str">字符串</param>
/// <returns>解码结果</returns>
public static string HtmlDecode(string str)
{
return HttpUtility.HtmlDecode(str);
}
/// <summary>
/// 检测是否有Sql危险字符
/// </summary>
/// <param name="str">要判断字符串</param>
/// <returns>判断结果</returns>
public static bool IsSafeSqlString(string str)
{
return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
}
/// <summary>
/// 检测用户登录。
/// </summary>
/// <param name="ID"></param>
/// <returns></returns>
public static string UserCheck(string username, string userpass)
{
string strsql = "select count(*) from Member where mem_Name='" + username + "' and mem_Password='" + userpass + "'";
OleDbConnection conn = DB.Getconn();
OleDbCommand com = new OleDbCommand(strsql, conn);
string hang = Convert.ToString(com.ExecuteScalar());
return hang;
}
}
这是我的数据库访问代码了!
@周星驰: 按提示信息看肯定是上面三种情况之一,仔细检查一下代码吧
xp本地测试吗?
连接对象非线程安全的,不要共享,很明显,你这个是并发冲突
请问这个需要如何修改?
那么把ACCESS换成SQLSERVER不知道是不是可以解决上述问题?
@周星驰: 没用的,你这个问题和数据库无关,详情请到 群 130707031 咨询
您有没有仔细看过您自己的代码,GetConn()每次创建一个新的连接,closeConnection()又调用GetConn(), 您到底要关闭哪个连接啊!
closeConnection()这个函数确实存在问题!
连接关闭释放资源
请问如何修改?能否给段修改代码!万分感谢!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
namespace ChouQian {
//工厂类,用于产生一个唯一的 数据库 对象
public class ChqDbFactory {
static private IChqDb db = new ExcelChqDb();
static public IChqDb getChqDb() {
if (!db.isOK())
db = new ExcelChqDb();
return db;
}
private ChqDbFactory() { }
}
//数据库信息的节点类
public class User {
public int id; //用户 ID
public string name; //用户姓名
public int zhi; //用户所在的值
public int stat; //用户所在的岗位
public int usedflag;//该用户是否已被抽取
override public string ToString() {
if (name == null) {
return "null";
}
return "name:" + name + " id:" + id + " zhi:" + zhi + " stat:" + stat + " usedflag:" + usedflag;
}
}
//数据库操作的接口,主要用于切换多种数据库实现
public interface IChqDb {
bool isOK(); //数据库是否成功连接
void close(); //关闭数据库
User[] getNames_NoUsed(int zhi, int stat); //取得某值某岗位还未被抽取到的人员名单
int AddUsedFlag(int id); //将该 id 的用户标记为已经抽取
int CleanAllUsedFlag(); //经已被抽取人员的标记清除
}
//数据库操作的接口的 EXCEL 实现
public class ExcelChqDb : IChqDb {
string path = "db.xls";
string strSheetName = "[renlist$]";
bool isOk = false;
OleDbConnection connection = null;
public ExcelChqDb() {
try {
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=2;'";
connection = new OleDbConnection(strConn);
connection.Open();
isOk = true;
} catch (Exception e) {
Msg("打开数据库时出现错误!" + e.Message);
}
}
public bool isOK() {
return isOk;
}
public void close() {
try {
if (connection != null) {
connection.Close();
}
} catch (DbException e) {
Msg("关闭数据库时出现错误!" + e.Message);
}
}
public User[] getNames_NoUsed(int zhi, int stat) {
List<User> list = new List<User>(20);
try {
string sql = "SELECT * FROM " + strSheetName + " WHERE (zhi=" + zhi + " and stat=" + stat + " and usedflag=0)";
OleDbCommand command = new OleDbCommand(sql, connection);
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read()) {
User user = new User();
user.id = Int32.Parse(reader["id"].ToString());
user.name = reader["name"].ToString();
list.Add(user);
}
reader.Close();
} catch (Exception e) {
Msg("执行查询时出现错误!" + e.Message);
}
return list.ToArray();
}
public int AddUsedFlag(int id) {
int result = 0;
string strUpdate = "UPDATE " + strSheetName + " SET usedflag=1 WHERE 增加使用标记时出现错误!" + e.Message);
}
return result;
}
public int CleanAllUsedFlag() {
int result = 0;
string str = "UPDATE " + strSheetName + " SET usedflag=0";
try {
OleDbCommand command = new OleDbCommand(str, connection);
result = command.ExecuteNonQuery();
} catch (DbException e) {
Msg("清除使用标记时出现错误!" + e.Message);
}
return result;
}
//消息显示框
static void Msg(string msg) {
System.Windows.Forms.MessageBox.Show(msg, "类 ExcelChqDb 中的提示信息",
System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
}
}
}