数据库操作类!

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.OleDb;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime;

namespace agoodidea
{
 /// <summary>
 /// agoodidea的数据操作类
 /// </summary>
 public class db:System.Web.UI.Page
 {
  #region 数据操作

  public OleDbConnection conn;
  public OleDbCommand cmd=new OleDbCommand();
  public OleDbDataReader dr;
  public OleDbDataAdapter ad=new OleDbDataAdapter();
  public DataSet ds;


  public db()
  {
   string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+this.Server.MapPath(".") + "//data//goodidea.mdb";
   conn=new OleDbConnection(ConnectionString);
   ad.SelectCommand=cmd;
  }

  /// <summary>
  /// 获得DataSet对象
  /// </summary>
  /// <param name="sql">sql语句</param>
  /// <returns>返回数据集</returns>
  public DataSet dataset(string sql)
  {
   conn.Open();
   ad=new OleDbDataAdapter(sql,conn);
   ds=new DataSet();
   ad.Fill(ds);
   conn.Close();
   return ds;
  }
  /// <summary>
  /// 获得DataTable对象
  /// </summary>
  /// <param name="sql">sql语句</param>
  /// <returns>返回数据表</returns>
  public DataTable table(string sql)
  {
   DataTable dt=new DataTable();
   try
   {
    cmd.CommandText=sql;
    cmd.Connection=conn;
    ad.Fill(dt);
    return dt;
   }
   finally
   {
    conn.Close();
   }
  }
  /// <summary>
  /// 操作数据库(插入和删除受影响的行数)
  /// </summary>
  /// <param name="sql">sql语句</param>
  /// <returns>返回受影响数</returns>
  public int handle(string sql)
  {
   cmd.CommandText=sql;
   cmd.Connection=conn;
   try
   {
    conn.Open();
    int i=cmd.ExecuteNonQuery();
    conn.Close();
    return i;
   }
   finally
   {
    conn.Close();
   }
  }

  /// <summary>
  /// select 一条数据成功是返回真,否则为假!
  /// </summary>
  /// <param name="sql">sql语句</param>
  /// <returns>操作成功返回真</returns>
  public bool excute(string sql)
  {
   try
   {
    conn.Open();
    cmd.CommandText=sql;
    cmd.Connection=conn;
   
    dr=cmd.ExecuteReader();
    return dr.Read();
   }
   finally
   {
    conn.Close();
   }
  }
  #endregion 数据操作结束

  #region 脚本操作
  
  /// <summary>
  /// 服务器端弹出alert对话框
  /// </summary>
  /// <param name="str_Message">提示信息</param>
  /// <param name="url">跳转地址</param>
  /// <param name="page">Page类</param>
  public void Alert(string str_Message,string url,Page page)
  {
   page.RegisterStartupScript("","<script>alert('"+str_Message+"');this.location.href='"+url+"';</script>");
  }  

/// <summary>
  /// 服务器端弹出alert对话框
  /// </summary>
  /// <param name="str_Message">提示信息,例子:"请输入您姓名!"</param>
  /// <param name="page">Page类</param>
  public void Alert(string str_Message,Page page)
  {
   page.RegisterStartupScript("","<script>alert('"+str_Message+"');</script>");
  }
  #endregion

暂时就这些,以后再补充!

展开阅读全文

没有更多推荐了,返回首页