温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:替某公司写的一个小论坛(供新人参考)
当前文件:
lingdaBBS/App_Code/DataBaseLayer/DataBase.cs[4K,2009-6-12 11:47:01],打开代码结构图
lingdaBBS/App_Code/DataBaseLayer/DataBase.cs[4K,2009-6-12 11:47:01],打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Data.OleDb; 5
using System.Web; 6
using System.Collections; 7
/// Download from www.51aspx.com(51aspx.com) 8
9
namespace DataBaseLayer 10
{ 11
public class DataBase:System.Web.UI.Page 12
{ 13
//数据库连接变量 14
private OleDbConnection con=null; 15
//数据库连接字符串 16
private string accString; 17
public DataBase() 18
{ 19
//初始化字符串 20
accString = "Provider=Microsoft.Jet.OleDb.4.0;"; 21
accString += "Data Source=" + System.Web.HttpContext.Current.Server.MapPath("lingda.mdb;"); 22
23
} 24
25
//打开数据库连接 26
public void open() 27
{ 28
if (con == null) 29
{ 30
con = new OleDbConnection(accString); 31
} 32
if (con.State.Equals(ConnectionState.Closed)) 33
{ 34
con.Open(); 35
} 36
} 37
38
//关闭数据库 39
public void close() 40
{ 41
if (con.State.Equals(ConnectionState.Open)) 42
{ 43
con.Close(); 44
45
//注销资源 46
con.Dispose(); 47
} 48
else 49
{ 50
con.Dispose(); 51
} 52
} 53
54
//执行SQL语句 55
public int ExecuteSQL(string sqlStr) 56
{ 57
try 58
{ 59
//打开连接 60
this.open(); 61
OleDbCommand cmd = new OleDbCommand(sqlStr, con); 62
return cmd.ExecuteNonQuery(); 63
} 64
catch 65
{ 66
return -1; 67
} 68
finally 69
{ 70
close(); 71
} 72
} 73
74
//执行SQL语句,返回查询的表 75
public DataTable GetDataTable(string sqlStr) 76
{ 77
DataTable dt; 78
DataSet ds; 79
try 80
{ 81
open(); 82
OleDbDataAdapter oda = new OleDbDataAdapter(sqlStr, con); 83
ds = new DataSet(); 84
oda.Fill(ds); 85
dt = ds.Tables[0]; 86
} 87
catch(Exception ex) 88
{ 89
dt=null; 90
} 91
finally 92
{ 93
close(); 94
} 95
return dt; 96
} 97
98
//执行SQL语句,返回数据集 99
public DataSet GetDataSet(string sqlStr) 100
{ 101
DataSet ds; 102
try 103
{ 104
open(); 105
OleDbDataAdapter oda = new OleDbDataAdapter(sqlStr, con); 106
ds = new DataSet(); 107
oda.Fill(ds); 108
} 109
catch 110
{ 111
return null; 112
} 113
finally 114
{ 115
close(); 116
} 117
return ds; 118
} 119
120
//执行SQL语句,返回DataRow 121
public DataRow GetDataRow(string sqlStr) 122
{ 123
DataRow dr; 124
try 125
{ 126
//调用该类GetDataTable方法 127
dr = GetDataTable(sqlStr).Rows[0]; 128
} 129
catch 130
{ 131
dr = null; 132
} 133
finally 134
{ 135
close(); 136
} 137
return dr; 138
} 139
140
//调用事务 141
public bool ExecuteTransaction(ArrayList sqlStrings) 142
{ 143
bool success = true; 144
OleDbTransaction tran = con.BeginTransaction(); 145
OleDbCommand cmd = new OleDbCommand(); 146
cmd.Connection = con; 147
cmd.Transaction = tran; 148
try 149
{ 150
foreach (string sqlStr in sqlStrings) 151
{ 152
cmd.CommandText = sqlStr; 153
cmd.ExecuteNonQuery(); 154
} 155
tran.Commit(); 156
success = true; 157
} 158
catch 159
{ 160
tran.Rollback(); 161
success = false; 162
} 163
finally 164
{ 165
close(); 166
} 167
return success; 168
} 169
} 170
} 171






}
}