C#技术百科
问问你的心你有没有信心 做事情要专一坚定,执着

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

namespace webdb.classes
{
 /// <summary>
 /// DbAccess 的摘要说明。
 /// </summary>
 public class DbAccess:Page
 {
  string ConnString;
  OleDbConnection conn ;
  OleDbCommand cmd;
  public DbAccess(string datapath)
  {
   
   ConnString = "provider=Microsoft.Jet.OleDb.4.0;Data Source="+HttpContext.Current.Request.PhysicalApplicationPath.ToString()+"file://"+datapath/;
   /*if(path.Length<1)
   {
    ConnString = "provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath("data/shuhua.mdb");
  
   }
   else
   {
    ConnString = "provider=Microsoft.Jet.OleDb.4.0;Data Source="+path[0].Trim();
   }*/
   conn = new OleDbConnection();
   conn.ConnectionString=ConnString;
   cmd = new OleDbCommand();
   cmd.Connection=conn;
   
  }
  /// <summary>
  /// 获取数据存在DataTable中
  /// </summary>
  /// <param name="sql"></param>
  /// <returns></returns>
  public DataTable GetTable(string sql)
  {
   cmd.CommandText=sql;
   OleDbDataAdapter da = new OleDbDataAdapter();
   da.SelectCommand=cmd;
   //DataTable dt = new DataTable();
   DataSet ds = new DataSet();
   da.Fill(ds);
   return ds.Tables[0];
  }
  /// <summary>
  /// 获取一个值
  /// </summary>
  /// <param name="sql"></param>
  /// <returns></returns>
  public string GetOne(string sql)
  {
   string obj="";
   conn.Open();
   cmd.CommandText=sql;
   try
   {
       obj = Convert.ToString(cmd.ExecuteScalar());
    obj = (obj==null)?(""):(obj);
    
   }
   catch(Exception ex)
   {
    HttpContext.Current.Response.Write("<script>alert('"+ex.Message.ToString()+"')</script>");
   }
   conn.Close();
   return obj;
  }
  public DataSet GetSet(string sql)
  {
   cmd.CommandText=sql;
   OleDbDataAdapter da = new OleDbDataAdapter();
   da.SelectCommand=cmd;
   //DataTable dt = new DataTable();
   DataSet ds = new DataSet();
   da.Fill(ds);
   return ds;
  }
  /// <summary>
  /// 事物处理
  /// </summary>
  /// <param name="sqls"></param>
  /// <returns></returns>
  public bool GetStateTran(params string[] sqls)
  {
   
   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 GetState(string sql)
  {
   bool suc = false;
   conn.Open();
   cmd.CommandText=sql;
   try
   {
    int count = cmd.ExecuteNonQuery();
    if(count>0)
     suc=true;
   }
   catch(Exception ex)
   {
    HttpContext.Current.Response.Write("<script>alert('"+ex.Message.ToString()+"')</script>");
   }
   conn.Close();
   return suc;

  }
 /* public bool ReturnState(string sql)
  {
   string String = "provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath("data/shuhua.mdb");
   OleDbConnection connR = new OleDbConnection();
   connR.ConnectionString=String;
   OleDbcommand cmdR = new OleDbCommand();
   cmdR.Connection=connR;
   bool suc = false;
   connR.Open();
   cmdR.CommandText=sql;
   try
   {
    int count = cmdR.ExecuteNonQuery();
    if(count>0)
     suc=true;
   }
   catch(Exception ex)
   {
    HttpContext.Current.Response.Write("<script>alert('"+ex.Message.ToString()+"')</script>");
   }
   connR.Close();
   return suc;

  }*/
  //分页
  /*public DataTable GetPerData(string sql,int cur)
  {
   cmd.CommandText=sql;
   OleDbDataAdapter da = new OleDbDataAdapter();
   da.SelectCommand=cmd;
   DataSet ds = new DataSet();
   int zon = cur*8;
   
   da.Fill(ds,zon,8,"per");
   
   return ds.Tables["per"];
  
  }
  */
  //根据提供的页数分页
  public DataTable GetPerData(string sql,int cur,int percount)
  {
   cmd.CommandText=sql;
   OleDbDataAdapter da = new OleDbDataAdapter();
   da.SelectCommand=cmd;
   DataSet ds = new DataSet();
   int zon = cur*percount;
   
   da.Fill(ds,zon,percount,"per");
   
   return ds.Tables["per"];
  }

 }
}

posted on 2008-06-10 14:13  王德田  阅读(138)  评论(0编辑  收藏  举报