首页 > 后端开发 > C#.Net教程 > 分享一个ASP.NET数据库操作类的实例

分享一个ASP.NET数据库操作类的实例

Y2J
发布: 2017-04-28 10:35:45
原创
1514 人浏览过

这篇文章主要介绍了ASP.NET数据库操作类,结合实例形式分析了asp.net基本的数据库增删改查操作封装相关技巧,需要的朋友可以参考下

本文实例讲述了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数据库操作类的实例的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板