温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:我的Asp.net三层聊天室源码
当前文件路径:MyChatRoom/App_Code/DataAccessLayer/Database.cs

1/********************************************************** 2
* 说明:MyChatRoom的数据操作层 3
* 作者: 4
* 创建日期: 5
*********************************************************/ 6
//51-A-s-p-x.com 7
8
using System; 9
using System.ComponentModel; 10
using System.Collections; 11
using System.Diagnostics; 12
using System.Data; 13
using System.Data.SqlClient; 14
using System.Configuration; 15
16
17
namespace MyChatRoom.DataAccessLayer 18
{ 19
/// <summary> 20
/// 数据访问类 21
/// </summary> 22
public class Database : IDisposable 23
{ 24
/// <summary> 25
/// 保护变量,数据库连接。 26
/// </summary> 27
protected SqlConnection Connection; 28
29
/// <summary> 30
/// 保护变量,数据库连接串。 31
/// </summary> 32
protected String ConnectionString; 33
34
/// <summary> 35
/// 构造函数。 36
/// </summary> 37
/// <param name="DatabaseConnectionString">数据库连接串</param> 38
public Database() 39
{ 40
ConnectionString = ConfigurationManager.AppSettings["DBConnectionString"]; 41
} 42
43
/// <summary> 44
/// 析构函数,释放非托管资源 45
/// </summary> 46
~Database() 47
{ 48
try 49
{ 50
if (Connection != null) 51
Connection.Close(); 52
} 53
catch{} 54
try 55
{ 56
Dispose(); 57
} 58
catch{} 59
} 60
61
/// <summary> 62
/// 保护方法,打开数据库连接。 63
/// </summary> 64
protected void Open() 65
{ 66
if (Connection == null) 67
{ 68
Connection = new SqlConnection(ConnectionString); 69
} 70
if (Connection.State.Equals(ConnectionState.Closed)) 71
{ 72
Connection.Open(); 73
} 74
} 75
76
/// <summary> 77
/// 公有方法,关闭数据库连接。 78
/// </summary> 79
public void Close() 80
{ 81
if (Connection != null) 82
Connection.Close(); 83
} 84
85
/// <summary> 86
/// 公有方法,释放资源。 87
/// </summary> 88
public void Dispose() 89
{ 90
// 确保连接被关闭 91
if (Connection != null) 92
{ 93
Connection.Dispose(); 94
Connection = null; 95
} 96
} 97
98
/// <summary> 99
/// 公有方法,获取数据,返回一个DataSet。 100
/// </summary> 101
/// <param name="SqlString">Sql语句</param> 102
/// <returns>DataSet</returns> 103
public DataSet GetDataSet(String SqlString) 104
{ 105
Open(); 106
SqlDataAdapter adapter = new SqlDataAdapter(SqlString,Connection); 107
DataSet dataset = new DataSet(); 108
adapter.Fill(dataset); 109
Close(); 110
return dataset; 111
} 112
113
/// <summary> 114
/// 公有方法,获取数据,返回一个DataRow。 115
/// </summary> 116
/// <param name="SqlString">Sql语句</param> 117
/// <returns>DataRow</returns> 118
public DataRow GetDataRow(String SqlString) 119
{ 120
DataSet dataset = GetDataSet(SqlString); 121
dataset.CaseSensitive = false; 122
if (dataset.Tables[0].Rows.Count>0) 123
{ 124
return dataset.Tables[0].Rows[0]; 125
} 126
else 127
{ 128
return null; 129
} 130
} 131
132
/// <summary> 133
/// 公有方法,执行Sql语句。 134
/// </summary> 135
/// <param name="SqlString">Sql语句</param> 136
/// <returns>对Update、Insert、Delete为影响到的行数,其他情况为-1</returns> 137
public int ExecuteSQL(String SqlString) 138
{ 139
int count = -1; 140
Open(); 141
try 142
{ 143
SqlCommand cmd = new SqlCommand(SqlString,Connection); 144
count = cmd.ExecuteNonQuery(); 145
} 146
catch 147
{ 148
count = -1; 149
} 150
finally 151
{ 152
Close(); 153
} 154
return count; 155
} 156
157
} 158
} 159



* 说明:MyChatRoom的数据操作层
********************************************************

