欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET数据库操作类实例

程序员文章站 2023-11-29 21:50:58
本文实例讲述了asp.net数据库操作类。分享给大家供大家参考,具体如下: using system; using system.data; using sy...

本文实例讲述了asp.net数据库操作类。分享给大家供大家参考,具体如下:

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.data.sqlclient;
namespace mysqlserver
{
  /// <summary>
  /// sqlserverdatabase 的摘要说明
  /// </summary>
  public class sqlserverdatabase
  {
    private string strerror = null;
    private int intcount = 0;
    public sqlserverdatabase()
    {
      //
      // todo: 在此处添加构造函数逻辑
      //
    }
    /// <summary>
    /// 公开方法dbconn,返回数据库连接
    /// </summary>
    /// <returns></returns>
    public sqlconnection dbconn()
    {
      string strconn = "server=(local);database=globalmeetings;uid=sa;pwd=";
      try
      {
        return new sqlconnection(strconn);
      }
      catch (exception)
      {
        return null;
      }
    }
    /// <summary>
    /// 公开属性errormessage,返回错误信息
    /// </summary>
    public string errormessage
    {
      get
      {
        return strerror;
      }
    }
    /// <summary>
    /// 根据查询语句从数据库检索数据
    /// </summary>
    /// <param name="strselect">查询语句</param>
    /// <param name="sqlconn">数据库连接</param>
    /// <returns>有数据则返回dataset对象,否则返回null</returns>
    public dataset select(string selectstring, sqlconnection sqlconn)
    {
      strerror = "";
      sqlconnection conn;
      if (sqlconn == null)
      {
        conn = dbconn();
      }
      else
      {
        conn = sqlconn;
      }
      try
      {
        //若数据库连接的当前状态是关闭的,则打开连接
        if (conn.state == connectionstate.closed)
        {
          conn.open();
        }
        sqldataadapter mysqldataadapter = new sqldataadapter();
        sqlcommand selectcommand = new sqlcommand(selectstring, conn);
        selectcommand.commandtype = commandtype.text;
        mysqldataadapter.selectcommand = selectcommand;
        dataset myds = new dataset();
        mysqldataadapter.fill(myds);
        return myds;
      }
      catch (exception e)
      {
        strerror = "数据检索失败:" + e.message;
        return null;
      }
      finally
      {
        if (conn.state != connectionstate.closed)
        {
          conn.close();
        }
      }
    }
    /// <summary>
    /// 更新数据库
    /// </summary>
    /// <param name="updatestring">update sql语句</param>
    /// <param name="sqlconn">数据库连接</param>
    /// <returns>更新成功返回true</returns>
    public bool update(string updatestring, sqlconnection sqlconn)
    {
      return udidatabase(updatestring, sqlconn);
    }
    /// <summary>
    /// 从数据库中删除数据
    /// </summary>
    /// <param name="deletestring">delete sql语句</param>
    /// <param name="sqlconn">数据库连接</param>
    /// <returns>删除成功返回true</returns>
    public bool delete(string deletestring, sqlconnection sqlconn)
    {
      return udidatabase(deletestring, sqlconn);
    }
    /// <summary>
    /// 把数据插入数据库
    /// </summary>
    /// <param name="insertstring">insert sql语句</param>
    /// <param name="sqlconn">数据库连接</param>
    /// <returns>插入成功返回true</returns>
    public bool insert(string insertstring, sqlconnection sqlconn)
    {
      return udidatabase(insertstring, sqlconn);
    }
    /// <summary>
    /// 根据sql语句更新数据库
    /// </summary>
    /// <param name="udistring">更新语句</param>
    /// <param name="sqlconn">数据库连接</param>
    /// <returns>更新成功则返回true</returns>
    public bool udidatabase(string udistring, sqlconnection sqlconn)
    {
      strerror = "";
      sqlconnection conn;
      if (sqlconn == null)
      {
        conn = dbconn();
      }
      else
      {
        conn = sqlconn;
      }
      try
      {
        if (conn.state == connectionstate.closed)
        {
          conn.open();
        }
        sqlcommand cmd = new sqlcommand(udistring, conn);
        cmd.commandtype = commandtype.text;
        intcount = cmd.executenonquery();
        return !(intcount < 1);
      }
      catch (exception e)
      {
        strerror = "更新数据库失败:" + e.message;
        return false;
      }
      finally
      {
        if (conn.state != connectionstate.closed)
        {
          conn.close();
        }
      }
    }
  }
}

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net优化技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作xml技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。