部分代码:
public OleDbConnection Con()
{
OleDbConnection DbCon = new OleDbConnection();
string strConnection = "Provider = Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source = "+Server.MapPath ("DB/company.mdb");
DbCon.ConnectionString = strConnection; return DbCon;
}
public DataSet ReDataSet(string quyString)
{
DataSet ds = new DataSet();
OleDbCommand SelCom = new OleDbCommand();
OleDbDataAdapter DBada = new OleDbDataAdapter();
DBada.SelectCommand.Connection = Con();
Con().Open();
DBada.SelectCommand = SelCom;
DBada.SelectCommand.CommandText=quyString; DBada.SelectCommand.ExecuteNonQuery();
DBada.Fill(ds);
return ds;
}
public OleDbConnection Con()
{
OleDbConnection DbCon = new OleDbConnection();
string strConnection = "Provider = Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source = "+Server.MapPath ("DB/company.mdb");
DbCon.ConnectionString = strConnection; return DbCon;
}
public DataSet ReDataSet(string quyString)
{
DataSet ds = new DataSet();
OleDbCommand SelCom = new OleDbCommand();
OleDbDataAdapter DBada = new OleDbDataAdapter();
DBada.SelectCommand.Connection = Con();
Con().Open();
DBada.SelectCommand = SelCom;
DBada.SelectCommand.CommandText=quyString; DBada.SelectCommand.ExecuteNonQuery();
DBada.Fill(ds);
return ds;
}
DBada.SelectCommand.Connection = Con();
Con().Open();
每次都新建了一个对象。并不是引用的同一个。
正确的代码:
public OleDbConnection Con()
{
OleDbConnection DbCon = new OleDbConnection();
string strConnection = "Provider = Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source = "+Server.MapPath("DB/company.mdb");
DbCon.ConnectionString = strConnection;
DbCon.Open(); return DbCon;
} public DataSet ReDataSet(string quyString)
{
DataSet ds = new DataSet();
OleDbCommand SelCom = new OleDbCommand();
OleDbDataAdapter DBada = new OleDbDataAdapter();
DBada.SelectCommand = SelCom;
DBada.SelectCommand.Connection = Con();
DBada.SelectCommand.CommandText=quyString;
DBada.Fill(ds); return ds;
}
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace Haiwo
{
/// <summary>
/// DataBase 的摘要说明。
/// </summary>
public class DataBase : System.Web.UI.Page
{
private System.Data.OleDb.OleDbCommand DBCom;
public System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
public System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
public System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
public System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
private System.Data.OleDb.OleDbDataAdapter DBAda;
private System.Data.OleDb.OleDbConnection DBCon;
/// <summary>
/// 必需的设计器变量。
/// </summary>
public DataBase(System.ComponentModel.IContainer container)
{
///
/// Windows.Forms 类撰写设计器支持所必需的
///
container.Add(this);
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} public DataBase()
{
///
/// Windows.Forms 类撰写设计器支持所必需的
///
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 操作数据
/// </summary>
/// <param name="SqlString">SQL命令</param>
/// <returns>如果操作成功返回真,否则返回假</returns>
public bool OperDB(string SqlString)
{
// try
// {
OleDbCommand SqlCom = new OleDbCommand();
SqlCom.Connection = Con();
// Con().Open();
SqlCom.CommandText = SqlString;
SqlCom.ExecuteNonQuery();
// }
// catch
// {
// return false;
// } return true;
}
/// <summary>
/// 返回数据库连接
/// </summary>
/// <returns></returns>
public OleDbConnection Con()
{
OleDbConnection DbCon = new OleDbConnection();
string strConnection = "Provider = Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source = "+Server.MapPath("DB/company.mdb");
DbCon.ConnectionString = strConnection;
DbCon.Open(); return DbCon;
} /// <summary>
/// 以DataSet形式返回,所查询的数据
/// </summary>
/// <param name="quyString">查询字符串</param>
/// <returns></returns>
public DataSet ReDataSet(string quyString)
{
DataSet ds = new DataSet();
OleDbCommand SelCom = new OleDbCommand();
OleDbDataAdapter DBada = new OleDbDataAdapter();
DBada.SelectCommand = SelCom;
DBada.SelectCommand.Connection = Con();
DBada.SelectCommand.CommandText=quyString;
DBada.Fill(ds); return ds;
} #region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DBCom = new System.Data.OleDb.OleDbCommand();
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
this.DBAda = new System.Data.OleDb.OleDbDataAdapter();
this.DBCon = new System.Data.OleDb.OleDbConnection();
//
// DBAda
//
this.DBAda.DeleteCommand = this.oleDbDeleteCommand1;
this.DBAda.InsertCommand = this.oleDbInsertCommand1;
this.DBAda.SelectCommand = this.oleDbSelectCommand1;
this.DBAda.UpdateCommand = this.oleDbUpdateCommand1; }
#endregion
}
}