数据连接类
using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
namespace Too.CommonLibrary
{
public class LinkDataBase
{
private string strSQL;
//与SQL Server的连接字符串设置
public string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=HuaYunProjectManagementDB.mdb";
//与数据库的连接
private OleDbConnection myConnection;
private OleDbCommandBuilder oleCmdBld;
private DataSet ds = new DataSet();
private OleDbDataAdapter da;
public LinkDataBase()
{
this.connectionString =
string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", Path.Combine(System.Environment.CurrentDirectory,
"HuaYunProjectManagementDB.mdb"));
}
//根据输入的SQL语句检索数据库数据
public DataSet SelectDataBase(string tempStrSQL, string tempTableName)
{
this.strSQL = tempStrSQL;
this.myConnection = new OleDbConnection(connectionString);
this.da = new OleDbDataAdapter(this.strSQL, this.myConnection);
this.ds.Clear();
this.da.Fill(ds, tempTableName);
return ds;//返回填充了数据的DataSet,其中数据
以tempTableName给出
的字符串命名
}
//数据库数据更新(传DataSet和DataTable表名对象)
public DataSet UpdateDataBase(DataSet changedDataSet, string tableName)
{
this.myConnection = new OleDbConnection(connectionString);
this.da = new OleDbDataAdapter(this.strSQL, this.myConnection);
this.oleCmdBld = new OleDbCommandBuilder(da);
this.da.Update(changedDataSet, tableName);
return changedDataSet;//返回更新了的数据库表
}
//检索数据库数据(传字符串,直接操作数据库)
public DataTable SelectDataBase(string tempStrSQL)
{
this.myConnection = new OleDbConnection(connectionString);
DataSet tempDataSet = new DataSet();
this.da = new OleDbDataAdapter(tempStrSQL, this.myConnection);
this.da.Fill(tempDataSet);
return tempDataSet.Tables[0];
}
//数据库数据更新(传字符串,直接操作数据库)
public int UpdateDataBase(string tempStrSQL)
{
this.myConnection = new OleDbConnection(connectionString);
//使用Command之前一定要先打开连接,后关闭连接,而DataAdapter则会
自动打开关闭连接
myConnection.Open();
OleDbCommand tempOleCommand = new OleDbCommand(tempStrSQL,
this.myConnection);
int intNumber = tempOleCommand.ExecuteNonQuery();//返回数据库中影响
的行数
myConnection.Close();
return intNumber;
}
}
}