connectionstring属性尚未初始化的问题!!!真的找不出原因了?
代码如下:
public class DbObject
{
private static string ConnectionString;
private OleDbConnection Conn;
private OleDbCommand Comm;
private OleDbDataReader Reader;
protected OleDbDataAdapter Adapter;
protected DataSet Ds;
public DbObject()
{
ConnectionString=ConfigurationManager.ConnectionStrings[ "ConnString "].ConnectionString+HttpContext.Current.Server.MapPath(ConfigurationManager.ConnectionStrings[ "ConnData "].ConnectionString);
Conn = new OleDbConnection(ConnectionString);
Comm = new OleDbCommand();
Comm.Connection = Conn;
//Adapter = new OleDbDataAdapter();
//Adapter.SelectCommand.Connection = Conn;
Ds = new DataSet();
}
/// <summary>
/// 释放连接及内存资源
/// </summary>
protected void Dispose()
{
if (Conn != null)
{
Conn.Close();
Conn.Dispose();
}
if (Comm != null)
{
Comm.Dispose();
}
//if (Ds != null)
//{
// Ds.Dispose();
//}
}
/// <summary>
/// 执行Sql语句,无返回值
/// </summary>
/// <param name= "sql "> sql语句 </param>
protected void ExecuteNonQuery(string sql)
{
Conn.Open();
Comm.CommandText = sql;
Comm.ExecuteNonQuery();
Dispose();
}
/// <summary>
/// 执行带OleDbParameter数组的sql语句
/// </summary>
/// <param name= "sql "> 要执行的sql语句 </param>
/// <param name= "op "> OleDbParameter数组 </param>
protected void ExecuteNonQuery(string sql, OleDbParameter[] op)
{
Conn.Open();
Comm.CommandText = sql;
foreach (OleDbParameter p in op)
{
Comm.Parameters.Add(p);
}
Comm.ExecuteNonQuery();
Dispose();
}
/// <summary>
/// 执行sql语句,返回受影响的行数
/// </summary>
/// <param name= "sql "> 要执行的sql语句 </param>
/// <returns> 返回受影响的行数 </returns>
protected int ExecuteScalar(string sql)
{
int returnValue;
Conn.Open();
Comm.CommandText = sql;
returnValue = (int)Comm.ExecuteScalar();
Dispose();
return returnValue;
}
protected DataView FillDateSet(string sql, int startRecord, int maxRecords)
{
Conn.Open(); //问题所在处
Adapter = new OleDbDataAdapter(sql, Conn);
Adapter.Fill(Ds, startRecord, maxRecords, "List ");
Dispose();
return Ds.Tables[ "List "].DefaultView;
}
}
当我调用该类中其它方法时一切正常.只用调用FillDataset方法时提未该错误!!
请问问题出在哪里!!
[解决办法]
ConnectionString=ConfigurationManager.ConnectionStrings[ "ConnString "].ConnectionString+HttpContext.Current.Server.MapPath(ConfigurationManager.ConnectionStrings[ "ConnData "].ConnectionString);
请检测WEB.CONFIG是,connectionStrings里,是否有ConnString这一节。
另外,是否有ConnData这一节。
------解决方案--------------------
你跟踪一下..不就得了...
[解决办法]
試試:
if(Conn==null) Conn = new OleDbConnection(ConnectionString);
if(Conn.State!=ConnectionState.Open) Conn.Open();
//other code