.net连access封装

using System;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.UI;

namespace shuhua.Db
{
/// <summary>
/// DbAccess 的摘要说明。
/// </summary>
public class DbAccess:Page
{
string ConnString;
OleDbConnection conn ;
OleDbCommand cmd;
public DbAccess()
{

ConnString = "provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath("data/shuhua.mdb");//+path.Path();;//+HttpContext.Current.Request.MapPath("../data/shuhua.mdb");

//HttpContext.Current.Response.Write("<script>alert('"+Server.MapPath("data/shuhua.mdb")+"')</script>");
conn = new OleDbConnection();
conn.ConnectionString=ConnString;
cmd = new OleDbCommand();
cmd.Connection=conn;

}
public DataTable GetDataTable(string sql)
{
cmd.CommandText=sql;
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand=cmd;
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
/// <summary>
/// 事物处理
/// </summary>
/// <param name="sqls"></param>
/// <returns></returns>
public bool ReturnState(params string[] sqls)
{
bool succ = false;
conn.Open();
OleDbTransaction tran = conn.BeginTransaction();
//tran.
cmd.Transaction=tran;
for(int i=0;i<sqls.Length;i++)
{
try
{
cmd.CommandText=sqls[i];
cmd.ExecuteNonQuery();

}
catch(Exception ex)
{
tran.Rollback();
HttpContext.Current.Response.Write(ex.Message.ToString());
return false;
}
}

tran.Commit();
conn.Close();
return true;
}
public bool ReturnState(string sql)
{
bool suc = false;
conn.Open();
cmd.CommandText=sql;
int count = cmd.ExecuteNonQuery();
if(count>0)
suc=true;
conn.Close();
return suc;

}
}
}

你可能感兴趣的:(sql,.net,Web,SQL Server,Access)