System.Data.OleDb.OleDbException: 未指定的错误

rczjp 2008-07-16 03:37:13
    public OleDbDataReader ReadRow(OleDbConnection myConnection, string strSql)
{
myConnection = new OleDbConnection(connectionString);
myConnection.Open();
OleDbCommand myCommand = new OleDbCommand(strSql, myConnection);
OleDbDataReader dr = myCommand.ExecuteReader();
if (dr.Read())
{
myCommand.Dispose();
return dr;
}
else
{
myCommand.Dispose();
return null;
}
}

myConnection.Open();-----错误被定格在这行
就是网页来回测试一段时间之后就出现这情况,上次问了只能在调用函数的时候关闭连接 不过一段时间还是出现这错误
获得OleDbDataReader 关闭连接的问题网上也找了好几天了 一直没有解决 反正就是一段时间就出现这个问题 重新开启之后就又可以了
连接池满了应该 那应该这样正确关闭呢 望高手指点
...全文
148 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
rczjp 2008-07-16
  • 打赏
  • 举报
回复
太感谢大家了 结帖~~~
lfywy 2008-07-16
  • 打赏
  • 举报
回复
续上...

/// <summary>
/// 返回指定Sql语句的OleDbDataReader,请注意,在使用后请关闭本对象,同时将自动调用closeConnection()来关闭数据库连接
/// 方法关闭数据库连接
/// </summary>
/// <param name="sqlstr">传入的Sql语句</param>
/// <param name="dr">传入的ref DataReader 对象</param>
public static void DataReader(string sqlstr, ref OleDbDataReader dr)
{
try
{
openConnection();
comm.CommandText = sqlstr;
comm.CommandType = CommandType.Text;
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
try
{
if (dr != null && !dr.IsClosed)
dr.Close();
}
catch
{
}
finally
{
closeConnection();
}
}
}

/// <summary>
/// 返回指定Sql语句的DataSet
/// </summary>
/// <param name="sqlstr">传入的Sql语句</param>
/// <returns>DataSet</returns>
public static DataSet DataSet(string sqlstr)
{
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
closeConnection();
}
return ds;
}

/// <summary>
/// 返回指定Sql语句的DataSet
/// </summary>
/// <param name="sqlstr">传入的Sql语句</param>
/// <param name="ds">传入的引用DataSet对象</param>
public static void DataSet(string sqlstr, ref DataSet ds)
{
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
closeConnection();
}
}
/// <summary>
/// 返回指定Sql语句的DataTable
/// </summary>
/// <param name="sqlstr">传入的Sql语句</param>
/// <returns>DataTable</returns>
public static DataTable DataTable(string sqlstr)
{
OleDbDataAdapter da = new OleDbDataAdapter();
DataTable Datatable = new DataTable();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(Datatable);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
closeConnection();
}
return Datatable;
}

/// <summary>
/// 执行指定Sql语句,同时给传入DataTable进行赋值
/// </summary>
/// <param name="sqlstr">传入的Sql语句</param>
/// <param name="dt">ref DataTable dt </param>
public static void DataTable(string sqlstr, ref DataTable dt)
{
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
closeConnection();
}
}

/// <summary>
/// 返回指定Sql语句的DataView
/// </summary>
/// <param name="sqlstr">传入的Sql语句</param>
/// <returns>DataView</returns>
public static DataView DataView(string sqlstr)
{
OleDbDataAdapter da = new OleDbDataAdapter();
DataView dv = new DataView();
DataSet ds = new DataSet();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
dv = ds.Tables[0].DefaultView;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
closeConnection();
}
return dv;
}
}
lfywy 2008-07-16
  • 打赏
  • 举报
回复
给你一个链接数据库方法类:

/// <summary>
/// DataAccess 数据访问类
/// By Lify
/// </summary>
public class DataAccess:System.Web.UI.Page
{
#region 属性
protected static OleDbConnection conn = new OleDbConnection();
protected static OleDbCommand comm = new OleDbCommand();
#endregion
public DataAccess()
{
//init();
}
#region 内部函数 静态方法中不会执行DataAccess()构造函数

/// <summary>
/// 打开数据库连接
/// </summary>
private static void openConnection()
{
if (conn.State == ConnectionState.Closed)
{
//SysConfig.ConnectionString 为系统配置类中连接字符串

conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + System.Configuration.ConfigurationSettings.AppSettings.Get("AccessRoot").ToString() + "\";Persist Security Info=True";
comm.Connection = conn;
try
{
conn.Open();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}

/// <summary>
/// 关闭当前数据库连接
/// </summary>
private static void closeConnection()
{
if (conn.State == ConnectionState.Open)
conn.Close();
conn.Dispose();
comm.Dispose();
}
#endregion

/// <summary>
/// 执行Sql查询语句
/// </summary>
/// <param name="sqlstr">传入的Sql语句</param>
public static void ExecuteSql(string sqlstr)
{
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
comm.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
closeConnection();
}
}

/// <summary>
/// 执行Sql查询语句并返回第一行的第一条记录,返回值为object 使用时需要拆箱操作 -> Unbox
/// </summary>
/// <param name="sqlstr">传入的Sql语句</param>
/// <returns>object 返回值 </returns>
public static object ExecuteScalar(string sqlstr)
{
object obj = new object();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
obj = comm.ExecuteScalar();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
closeConnection();
}
return obj;
}

/// <summary>
/// 执行Sql查询语句,同时进行事务处理
/// </summary>
/// <param name="sqlstr">传入的Sql语句</param>
public static void ExecuteSqlWithTransaction(string sqlstr)
{
closeConnection();
openConnection();

OleDbTransaction trans;
trans = conn.BeginTransaction();
comm.Transaction = trans;
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
comm.ExecuteNonQuery();
trans.Commit();
}
catch
{
trans.Rollback();
}
finally
{
closeConnection();
}
}

/// <summary>
/// 返回指定Sql语句的OleDbDataReader,请注意,在使用后请关闭本对象,同时将自动调用closeConnection()来关闭数据库连接
/// 方法关闭数据库连接
/// </summary>
/// <param name="sqlstr">传入的Sql语句</param>
/// <returns>OleDbDataReader对象</returns>
public static OleDbDataReader DataReader(string sqlstr)
{
OleDbDataReader dr = null;
try
{
openConnection();
comm.CommandText = sqlstr;
comm.CommandType = CommandType.Text;
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
try
{
dr.Close();
closeConnection();
}
catch
{
}
}
return dr;
}
//待续 ......
路人乙e 2008-07-16
  • 打赏
  • 举报
回复
myCommand.ExecuteReader(CommandBehavior.CloseConnection);
tootto 2008-07-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 rczjp 的回复:]
C# codeprotectedvoidIPInsert()
{
IPAddress ipAddress=newIPAddress();stringuser_ip=ipAddress.GetIPAddress();stringSelectIPSql="Select IpAddress From ipinfo Where IpAddress='"+user_ip+"'";
OleDbDataReader dr=QinMiData.ReadRow(myConnection, SelectIPSql);if(dr!=null)
{stringUpdateIPSql="Update ipinfo Set AddTime='"+DateTime.Now+"' where IpAddress='"+user_ip+"'";

[/Quote]

建议用两个独立的方法,不要把read和update放在一个方法里。

protected bool HasIpExisted(string userIp)
{
bool hasIpExisted = false;

//define connection, command, reader

myReader = myCommand.ExecuteReader();

hasIpExisted = myReader.HasRow;

//close reader, connection

return hasIpExisted;
}

protected void UpdateIpAddress()
{
string sqlCommand = string.Empty;

if ( HasIpExisted(ipAddress.GetIPAddress()))
{
sqlCommand = "update xxxx xxxxx";
}
else
{
sqlCommand = "insert into xxxxx";
}

..........
..........

}

whoami333 2008-07-16
  • 打赏
  • 举报
回复
OleDbConnection con=new OleDBConnection("你的连接串");

不好意思,忘了。还有你的这个myConnection,最好要用的时候创建,用完即销毁,不要使用类似全局变量的这种方式。
whoami333 2008-07-16
  • 打赏
  • 举报
回复
protected void IPInsert()
{
IPAddress ipAddress = new IPAddress();
string user_ip = ipAddress.GetIPAddress();
string SelectIPSql = "Select IpAddress From ipinfo Where IpAddress='" + user_ip + "'";
OleDbConnection con=new OleDBConnection();
con.Open();
OleDbCommand cmd=new OleDbCommand(SelectIPSql,con);
OleDbDataReader dr = cmd.ExecuteNonQuery();

if (!dr.HasRows)
{
string UpdateIPSql = "Update ipinfo Set AddTime='" + DateTime.Now + "' where IpAddress='" + user_ip + "'";
QinMiData.ExecuteSql(UpdateIPSql);
//dr.Close();
//myConnection.Close();
//myConnection.Dispose();

}
else
{
string InsertIPSql = "Insert into ipinfo(IpAddress,AddTime) values('" + user_ip + "','" + DateTime.Now + "')";
QinMiData.ExecuteSql(InsertIPSql);
//dr.Close();
//myConnection.Close();
//myConnection.Dispose();

}
dr.Close();
con.Close();

}

改成这样吧。
一品梅 2008-07-16
  • 打赏
  • 举报
回复
没见你销毁connection
tootto 2008-07-16
  • 打赏
  • 举报
回复
我想问题出在你试图返回一个reader,这样做行不通。

reader是同步操作数据库,增量读取数据(读完一条数据接着下一条,无法再返回上一条数据),所以你返回一个reader这样做是毫无意义的。

返回一个DataSet object是正道。
rczjp 2008-07-16
  • 打赏
  • 举报
回复
恩 都可以正常运行的
主要就是一段时间的操作之后就出现那个问题 指示的错误就是OPEN那里。。
whoami333 2008-07-16
  • 打赏
  • 举报
回复
public OleDbDataReader ReadRow(OleDbConnection myConnection, string strSql)
{
myConnection = new OleDbConnection(connectionString); //把这行注释掉试一下。
myConnection.Open();
hongqi162 2008-07-16
  • 打赏
  • 举报
回复
connectionString里面有值么,输出一下看看,或者换个固定的路径试试
rczjp 2008-07-16
  • 打赏
  • 举报
回复
另外我的是ACCESS数据库 不知道是不是也有影响~~
rczjp 2008-07-16
  • 打赏
  • 举报
回复
    protected void IPInsert()
{
IPAddress ipAddress = new IPAddress();
string user_ip = ipAddress.GetIPAddress();
string SelectIPSql = "Select IpAddress From ipinfo Where IpAddress='" + user_ip + "'";
OleDbDataReader dr = QinMiData.ReadRow(myConnection, SelectIPSql);
if (dr != null)
{
string UpdateIPSql = "Update ipinfo Set AddTime='" + DateTime.Now + "' where IpAddress='" + user_ip + "'";
QinMiData.ExecuteSql(UpdateIPSql);
dr.Close();
myConnection.Close();
myConnection.Dispose();
}
else
{
string InsertIPSql = "Insert into ipinfo(IpAddress,AddTime) values('" + user_ip + "','" + DateTime.Now + "')";
QinMiData.ExecuteSql(InsertIPSql);
dr.Close();
myConnection.Close();
myConnection.Dispose();
}
}

比如上面这个 我都关闭了的 很奇怪一段时间之后就不行了
Jinglecat 2008-07-16
  • 打赏
  • 举报
回复
没见你的调用方法,没见你 close connection

110,825

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧