ASP.NET数据库操作类实例

时间:2022-09-17 21:34:34

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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程序设计有所帮助。