using
System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
namespace DBase
{
public class DataAccess
{
#region 定义全局变量
private OleDbConnection conn;
private OleDbDataAdapter adp;
private OleDbCommand cmd;
private DataSet ds = new DataSet();
#endregion
#region 构造函数初始化数据库连接
/// <summary>
/// 构造函数
/// </summary>
public DataAccess( string ConnectionString)
{
Open(ConnectionString);
}
public DataAccess()
{
}
#endregion
#region 打开关闭数据库连接
/// <summary>
/// 打开数据库连接
/// </summary>
protected void Open( string Constr)
{
if (conn == null || conn.State == ConnectionState.Closed)
{
Constr = System.Configuration.ConfigurationManager.AppSettings[ " conn " ] + Constr;
conn = new OleDbConnection(Constr);
conn.Open();
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
private void Close()
{
if (conn != null || conn.State == ConnectionState.Open )
{
conn.Close();
conn.Dispose();
conn = null ;
}
}
#endregion
#region 返回传入的SQL语句数据表(GetDataTable)
/// <summary>
/// 返回传入的SQL语句数据表
/// </summary>
/// <param name="Sql"> SQL语句 </param>
/// <param name="TableName"> 返回数据集的表名 </param>
public DataTable GetDataTable( string Sql)
{
adp = new OleDbDataAdapter(Sql,conn);
adp.Fill(ds);
Close();
return ds.Tables[ 0 ];
}
public DataTable GetDataTable( string Sql, string connstr)
{
Open(connstr);
adp = new OleDbDataAdapter(Sql, conn);
adp.Fill(ds);
Close();
return ds.Tables[ 0 ];
}
#endregion
#region 返回传入的SQL语句分页后的数据表(GetPagedDataTable)
/// <summary>
/// 返回传入的SQL语句分页后的数据表(GetPagedDataTable)
/// </summary>
/// <param name="Sql"> SQL语句 </param>
/// <param name="TableName"> 返回数据集的表名 </param>
public DataTable GetPagedDataTable( string Sql, int StartPage, int PageSize)
{
adp = new OleDbDataAdapter(Sql, conn);
adp.Fill(ds, StartPage * PageSize, PageSize, " PageData " );
Close();
return ds.Tables[ " PageData " ];
}
#endregion
#region 传入SQL语句,返回DataSet数据集(GetDataSet)
/// <summary>
/// 传入SQL语句,返回DataSet数据集
/// </summary>
/// <param name="strSQL"></param>
/// <returns></returns>
public DataSet GetDataSet( string Sql)
{
adp = new OleDbDataAdapter(Sql, conn);
ds = new DataSet();
adp.Fill(ds);
Close();
return ds;
}
#endregion
#region 返回SQL语句的第一列第一行的数据(GetScalarData)
/// <summary>
/// 返回SQL语句的第一列第一行的数据
/// </summary>
/// <param name="Sql"> SQL语句 </param>
/// <param name="TableName"> 返回数据集的表名 </param>
public string GetScalarData( string Sql)
{
string str;
try
{
this .cmd = new OleDbCommand(Sql,conn);
str = ( this .cmd.ExecuteScalar()).ToString();
cmd.Dispose();
Close();
}
catch
{
str = null ;
cmd.Dispose();
Close();
}
return str;
}
#endregion
#region 执行插入,删除,更新语句,返回Int类型,i为影响的行数(SetExcuteSql)
/// <summary>
/// 执行插入,删除,更新语句,返回Int类型,i为影响的行数
/// </summary>
/// <param name="Sql"> SQL语句 </param>
public int ExcuteSQL( string Sql)
{
int i = 0 ;
try
{
this .cmd = new OleDbCommand(Sql,conn);
i = this .cmd.ExecuteNonQuery();
cmd.Dispose();
Close();
}
catch
{
cmd.Dispose();
Close();
}
return i;
}
#endregion
#region 执行 SQL 语句,返回 1 表示结果中有记录,返回 0 表示结果中无记录(GetDataRecord)
/// <summary>
/// 执行 SQL 语句,返回 true 表示结果中有记录,返回 false 表示结果中无记录(GetDataRecord)
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public bool GetDataRecord( string sql)
{
try
{
cmd = new OleDbCommand(sql, conn);
OleDbDataReader myDr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (myDr.Read())
{
Close();
return true ;
}
else
{
Close();
return false ;
}
}
catch
{
Close();
return false ;
}
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
namespace DBase
{
public class DataAccess
{
#region 定义全局变量
private OleDbConnection conn;
private OleDbDataAdapter adp;
private OleDbCommand cmd;
private DataSet ds = new DataSet();
#endregion
#region 构造函数初始化数据库连接
/// <summary>
/// 构造函数
/// </summary>
public DataAccess( string ConnectionString)
{
Open(ConnectionString);
}
public DataAccess()
{
}
#endregion
#region 打开关闭数据库连接
/// <summary>
/// 打开数据库连接
/// </summary>
protected void Open( string Constr)
{
if (conn == null || conn.State == ConnectionState.Closed)
{
Constr = System.Configuration.ConfigurationManager.AppSettings[ " conn " ] + Constr;
conn = new OleDbConnection(Constr);
conn.Open();
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
private void Close()
{
if (conn != null || conn.State == ConnectionState.Open )
{
conn.Close();
conn.Dispose();
conn = null ;
}
}
#endregion
#region 返回传入的SQL语句数据表(GetDataTable)
/// <summary>
/// 返回传入的SQL语句数据表
/// </summary>
/// <param name="Sql"> SQL语句 </param>
/// <param name="TableName"> 返回数据集的表名 </param>
public DataTable GetDataTable( string Sql)
{
adp = new OleDbDataAdapter(Sql,conn);
adp.Fill(ds);
Close();
return ds.Tables[ 0 ];
}
public DataTable GetDataTable( string Sql, string connstr)
{
Open(connstr);
adp = new OleDbDataAdapter(Sql, conn);
adp.Fill(ds);
Close();
return ds.Tables[ 0 ];
}
#endregion
#region 返回传入的SQL语句分页后的数据表(GetPagedDataTable)
/// <summary>
/// 返回传入的SQL语句分页后的数据表(GetPagedDataTable)
/// </summary>
/// <param name="Sql"> SQL语句 </param>
/// <param name="TableName"> 返回数据集的表名 </param>
public DataTable GetPagedDataTable( string Sql, int StartPage, int PageSize)
{
adp = new OleDbDataAdapter(Sql, conn);
adp.Fill(ds, StartPage * PageSize, PageSize, " PageData " );
Close();
return ds.Tables[ " PageData " ];
}
#endregion
#region 传入SQL语句,返回DataSet数据集(GetDataSet)
/// <summary>
/// 传入SQL语句,返回DataSet数据集
/// </summary>
/// <param name="strSQL"></param>
/// <returns></returns>
public DataSet GetDataSet( string Sql)
{
adp = new OleDbDataAdapter(Sql, conn);
ds = new DataSet();
adp.Fill(ds);
Close();
return ds;
}
#endregion
#region 返回SQL语句的第一列第一行的数据(GetScalarData)
/// <summary>
/// 返回SQL语句的第一列第一行的数据
/// </summary>
/// <param name="Sql"> SQL语句 </param>
/// <param name="TableName"> 返回数据集的表名 </param>
public string GetScalarData( string Sql)
{
string str;
try
{
this .cmd = new OleDbCommand(Sql,conn);
str = ( this .cmd.ExecuteScalar()).ToString();
cmd.Dispose();
Close();
}
catch
{
str = null ;
cmd.Dispose();
Close();
}
return str;
}
#endregion
#region 执行插入,删除,更新语句,返回Int类型,i为影响的行数(SetExcuteSql)
/// <summary>
/// 执行插入,删除,更新语句,返回Int类型,i为影响的行数
/// </summary>
/// <param name="Sql"> SQL语句 </param>
public int ExcuteSQL( string Sql)
{
int i = 0 ;
try
{
this .cmd = new OleDbCommand(Sql,conn);
i = this .cmd.ExecuteNonQuery();
cmd.Dispose();
Close();
}
catch
{
cmd.Dispose();
Close();
}
return i;
}
#endregion
#region 执行 SQL 语句,返回 1 表示结果中有记录,返回 0 表示结果中无记录(GetDataRecord)
/// <summary>
/// 执行 SQL 语句,返回 true 表示结果中有记录,返回 false 表示结果中无记录(GetDataRecord)
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public bool GetDataRecord( string sql)
{
try
{
cmd = new OleDbCommand(sql, conn);
OleDbDataReader myDr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (myDr.Read())
{
Close();
return true ;
}
else
{
Close();
return false ;
}
}
catch
{
Close();
return false ;
}
}
#endregion
}
}