ACCESS数据库访问类封装

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.OleDb;


/// <summary>
/// AccessDB 的摘要说明
/// </summary>
public class AccessDB
{
public AccessDB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
// ORStr
private readonly string constr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ORStr"].ToString();//得到数据库联接(判断是什么联接)

public int ExecuteSQL(string sql)
{
int result = -1;
using (OleDbConnection conn = new OleDbConnection(constr))
{

OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
result = cmd.ExecuteNonQuery();
conn.Close();
}
return result;
}

/// <summary>
/// 查询操作
/// </summary>
/// <param name="sql"></param>
/// <param name="tableName"></param>
/// <returns></returns>
public System.Data.DataSet QuerySQL(string sql, string tableName)
{
DataSet ds = null;
OleDbDataAdapter dsa = null;
OleDbConnection conn = null;
try
{

{
conn = new OleDbConnection(constr);

dsa = new OleDbDataAdapter(sql, conn);

ds = new DataSet();
conn.Open();
dsa.Fill(ds, tableName);
conn.Close();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (conn != null)
{
conn.Close();
}
}
return ds;
}

System.Data.IDataReader ExecuteReader(string sql)
{
IDataReader dr = null;
return dr;
}

#region IDBCommon 成员


public int ExecuteSQL(string sql, params IDbDataParameter[] parameters)
{
int result = -1;
if (parameters != null)
{
using (OleDbConnection conn = new OleDbConnection(constr))
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
foreach (IDbDataParameter para in parameters)
{
cmd.Parameters.Add(para);
}
conn.Open();
result = cmd.ExecuteNonQuery();
conn.Close();
}
}
else
{

using (OleDbConnection conn = new OleDbConnection(constr))
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
result = cmd.ExecuteNonQuery();
conn.Close();
}

}
return result;
}

#endregion

#region IDBCommon 成员
/// <summary>
/// 打开连接
/// </summary>
/// <param name="strconn">连接字符串</param>
/// <param name="conn"></param>
/// <returns>执行是否成功</returns>

public int OpenConn(string strconn, IDbConnection conn)
{
int falge = -1;
try
{
if (conn != null)
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
conn = new OleDbConnection(strconn);
conn.Open();//打开连接
falge = 1;
}
catch (Exception ex)
{
falge = -1;
throw new Exception("_数据库连接失败_:" + ex.Message);//抛出错误信息
}
return falge;
}
#endregion

}

你可能感兴趣的:(Access)