我这个当个反面教材就好了。
嗨,辛苦地写了半天~~~草草了事了~~先发上来,大家帮忙看看有啥不好噢~!多提意见会对我很有帮助滴~~~!
目的:好好地写一个类处理大部分常用的oracle数据库连接的问题。
当然,在开始还是把基本设置做齐了,省得一会连不通被人骂

首先,配置web.config文件
添加



类文件名为conn.cs
由于VS.NET2005不默认支持oracle数据库,所以要添加其引用:在解决方案资源管理器里添加引用:System.Data.OracleClient(仔细找找噢~在.NET选项卡中)
内容(不断更新):
1
using
System;
2
using
System.Data;
3
using
System.Configuration;
4
using
System.Web;
5
using
System.Web.Security;
6
using
System.Web.UI;
7
using
System.Web.UI.WebControls;
8
using
System.Web.UI.WebControls.WebParts;
9
using
System.Web.UI.HtmlControls;
10
using
System.Data.OracleClient;
11
12
namespace
User.Data
13
{
14
public class conn
15
{
16
17
构造函数#region 构造函数
18
/**//// <summary>
19
/// 构造函数(默认)
20
/// </summary>
21
public conn()
{ }
22
23
/**//// <summary>
24
/// 构造函数
25
/// </summary>
26
/// <param name="connString">连接字符串(类型:string)</param>
27
/// 例子:"Data Source=ServerName;User ID=userid;Password=password;Unicode=True"
28
public conn(string connString)
29
{
30
this.ConnectionString = connString;
31
}
32
33
#endregion
34
35
私有变量#region 私有变量
36
37
/**//// <summary>
38
/// 表示针对数据库执行的SQL语句或存储过程
39
/// </summary>
40
private System.Data.OracleClient.OracleCommand cmd;
41
42
/**//// <summary>
43
/// 表示一个到数据库的打开的连接
44
/// </summary>
45
private System.Data.OracleClient.OracleConnection con;
46
47
/**//// <summary>
48
/// 表示要在数据库中生成的事务
49
/// </summary>
50
private System.Data.OracleClient.OracleTransaction tran;
51
52
/**//// <summary>
53
/// 表示用于填充System.Data.DataSet和更新数据库的一组数据命令和到数据库的连接。
54
/// </summary>
55
private System.Data.OracleClient.OracleDataAdapter adapter;
56
57
/**//// <summary>
58
/// 提供从数据源读取数据行的只进流的方法
59
/// </summary>
60
private System.Data.OracleClient.OracleDataReader reader;
61
62
/**//// <summary>
63
/// 表示内存中数据的一个表
64
/// </summary>
65
private System.Data.DataTable dt;
66
67
/**//// <summary>
68
/// 表示数据在内存中的缓存
69
/// </summary>
70
private System.Data.DataSet ds;
71
/**//// <summary>
72
/// 连接字符串
73
/// </summary>
74
string ConnectionString = "";
75
76
/**//// <summary>
77
/// 返回与打开的数据库连接
78
/// </summary>
79
private System.Data.OracleClient.OracleConnection openedCon
80
{
81
get
82
{
83
this.openCon(); //固定地打开数据库与之连接
84
return this.con;
85
}
86
//connectiongString是由固定的,存放在Web.config文件的AppSetting节点下,因此无需set访问器
87
}
88
#endregion
89
90
私有方法#region 私有方法
91
/**//// <summary>
92
/// 打开与数据库的连接
93
/// </summary>
94
private void openCon()
95
{
96
try
97
{
98
if (this.con == null)
99
{
100
//使用using可以使该连接可以调用Dispose方法来释放资源
101
//using (this.con = new OracleConnection())
102
//{
103
this.con = new OracleConnection();
104
//设置数据库连接属性为web.config中的设置的值(默认)
105
//或者设置为构造函数中指定的connString的值
106
this.con.ConnectionString
107
= (this.ConnectionString == "") ? System.Configuration.ConfigurationManager.AppSettings["SysDSN"] : this.ConnectionString;
108
this.con.Open();
109
//}
110
//System.Web.HttpContext.Current.Response.Write("数据库连接成功!"); //Test
111
}
112
else if (con.State == ConnectionState.Closed)
113
{
114
this.con.Open();
115
}
116
}
117
catch
118
{
119
System.Web.HttpContext.Current.Response.Write("数据库连接失败,请与管理员联系!");
120
System.Web.HttpContext.Current.Response.End();
121
}
122
}
123
124
/**//// <summary>
125
/// 获取或设置将在其中执行System.Data.OracleClient.OracleCommand的
126
/// System.Data.OracleClient.OracleTransaction。
127
/// 因为OracleConnection 不支持并行事务。所以在添加事务前必须要检查是否为空!
128
/// </summary>
129
private void checkTransaction()
130
{
131
if (this.tran != null)
132
{
133
this.cmd.Transaction = this.tran;
134
}
135
}
136
137
/**//// <summary>
138
/// 设置基本Command对象
139
/// </summary>
140
/// <param name="sql"></param>
141
private void CreateCmd(string sql)
142
{
143
//方法1
144
this.cmd = new OracleCommand();
145
this.checkTransaction();
146
this.cmd.Connection = this.openedCon;
147
this.cmd.CommandText = sql;
148
149
//方法2
150
//this.checkTransaction();
151
//this.cmd=new OracleCommand(sql);
152
//this.cmd.Connection=this.openedCon;
153
154
//方法3
155
//this.checkTransaction();
156
//this.cmd = new OracleCommand(sql, this.openedCon);
157
158
//方法4
159
//this.cmd = new OracleCommand(sql, this.openedCon, this.tran);
160
161
//方法5(Oracle中只支持.NET2.0以上版本)
162
//this.cmd = this.openedCon.CreateCommand();
163
//this.checkTransaction();
164
//this.cmd.CommandText = sql;
165
}
166
167
#endregion
168
169
公共方法#region 公共方法
170
171
/**//// <summary>
172
/// 开始事务
173
/// </summary>
174
public void BeginTransaction()
175
{
176
this.tran = this.openedCon.BeginTransaction();
177
}
178
/**//// <summary>
179
/// 提交事务
180
/// </summary>
181
public void Commit()
182
{
183
this.tran.Commit();
184
}
185
/**//// <summary>
186
/// 回滚事务
187
/// </summary>
188
public void RollBack()
189
{
190
this.tran.Rollback();
191
}
192
193
/**//// <summary>
194
/// 关闭与数据库的连接
195
/// </summary>
196
public void CloseCon()
197
{
198
if (this.openedCon != null && this.openedCon.State == ConnectionState.Open)
199
{
200
this.openedCon.Close();
201
}
202
}
203
204
/**//// <summary>
205
/// 执行SQL语句
206
/// </summary>
207
/// <param name="sql">SQL语句</param>
208
/// <returns>受影响的行数</returns>
209
public int DoSelectSql(string sql)
210
{
211
this.CreateCmd(sql);
212
return this.cmd.ExecuteNonQuery();
213
}
214
215
/**//// <summary>
216
/// 获得OracleDataReader对象
217
/// </summary>
218
/// <param name="sql">SQL语句</param>
219
/// <returns>OracleDataReader对象</returns>
220
public System.Data.OracleClient.OracleDataReader GetReader(string sql)
221
{
222
this.CreateCmd(sql);
223
this.reader=this.cmd.ExecuteReader();
224
return this.reader;
225
}
226
227
/**//// <summary>
228
/// 获得DataTable
229
/// </summary>
230
/// <param name="sql">SQL语句</param>
231
/// <returns>DataTable对象</returns>
232
public System.Data.DataTable GetDataTable(string sql)
233
{
234
this.adapter = new OracleDataAdapter();
235
this.dt = new DataTable();
236
this.CreateCmd(sql);
237
this.adapter.SelectCommand = this.cmd;
238
this.adapter.Fill(this.dt);
239
return this.dt;
240
}
241
242
/**//// <summary>
243
/// 获得DataSet(通过sql语句)
244
/// </summary>
245
/// <param name="sql">SQL语句</param>
246
/// <returns>DataSet对象</returns>
247
public System.Data.DataSet GetDataSet(string sql)
248
{
249
this.adapter = new OracleDataAdapter();
250
this.ds = new DataSet();
251
this.CreateCmd(sql);
252
this.adapter.SelectCommand = this.cmd;
253
this.adapter.Fill(this.ds);
254
return this.ds;
255
}
256
257
/**//// <summary>
258
/// 获得DataSet(通过sql语句、表名)
259
/// </summary>
260
/// <param name="sql">SQL语句</param>
261
/// <param name="srcTableName">用于表映射的源表的名称</param>
262
/// <returns>DataSet对象</returns>
263
public System.Data.DataSet GetDataSet(string sql,string srcTableName)
264
{
265
if (this.ds == null)
266
{
267
this.ds = new DataSet();
268
}
269
this.adapter = new OracleDataAdapter();
270
this.CreateCmd(sql);
271
this.adapter.SelectCommand = this.cmd;
272
this.adapter.Fill(ds,srcTableName);
273
return this.ds;
274
}
275
276
/**//// <summary>
277
/// 获得DataSet(通过sql语句、表名、已存在的DataSet)
278
/// </summary>
279
/// <param name="sql">SQL语句</param>
280
/// <param name="srcTableName">用于表映射的源表的名称</param>
281
/// <param name="DataSet">已存在的DataSet对象</param>
282
/// <returns>DataSet对象</returns>
283
public System.Data.DataSet GetDataSet(string sql, string srcTableName, DataSet DataSet)
284
{
285
this.ds = DataSet;
286
return GetDataSet(sql,srcTableName);
287
}
288
289
290
#endregion
291
}
292
}
293

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


172

173

174

175



176

177

178


179

180

181

182



183

184

185


186

187

188

189



190

191

192

193


194

195

196

197



198

199



200

201

202

203

204


205

206

207

208

209

210



211

212

213

214

215


216

217

218

219

220

221



222

223

224

225

226

227


228

229

230

231

232

233



234

235

236

237

238

239

240

241

242


243

244

245

246

247

248



249

250

251

252

253

254

255

256

257


258

259

260

261

262

263

264



265

266



267

268

269

270

271

272

273

274

275

276


277

278

279

280

281

282

283

284



285

286

287

288

289

290

291

292

293

最后就是调用它们了,不过这个不是本文的重点,就随便显示显示就OK了。
随便拉两个GridView控件,在它们的Page_Load事件中添加代码:(注意红色部分要和实际相符)



















基本上就OK了,Ctrl+F5就OK了。记得把两条sql语句改改~~~~`还有web.config中的连接名和连接密码要和实际的相符,否则不是连不上就是查不到东东噢~~~
conn.cs下载地址: http://files.cnblogs.com/volnet/conn[Oracle].rar