我的数据访问函数库的源码。整个类有1400行,原先就是分开来写的,现在更新后还是分开来发一下吧。
第三部分:返回结构
1
//新增加的部分,返回结构数组用于绑定控件
2
3
函数实现 — — RunSqlStructBaseTitle#region 函数实现 — — RunSqlStructBaseTitle
4
/**//// <summary>
5
/// 运行SQl语句返回结构数组BaseTitle
6
/// </summary>
7
/// <param name="SQL">查询语句。比如select myName from tableName</param>
8
/// <param name="SQLForCount">用于统计记录数的SQL语句。如果传入数字的话,则直接使用数字作为记录数。</param>
9
/// <param name="TitleCount">标题的字符数,一个汉字按照两个字符计算。传入“0”则表示不截取标题。</param>
10
/// <param name="DateFormat">时间格式。比如“yyyy-MM-dd HH:mm:ss dddd”</param>
11
/// <returns>返回BaseTitle结构的数组。URL,标题,时间,人气</returns>
12
public BaseTitle[] RunSqlStructBaseTitle(string SQL,string SQLForCount,int TitleCount,string DateFormat,int IntroCount)
13
{
14
string strRowCount = RunSqlGetID(SQLForCount);
15
if (strRowCount == null)
16
return null;
17
18
int DataCount = Int32.Parse(strRowCount);
19
if (DataCount <1)
20
return null;
21
22
return RunSqlStructBT(SQL,DataCount,TitleCount,DateFormat,IntroCount);
23
}
24
#endregion
25
26
函数实现 — — RunSqlStructBaseTitle#region 函数实现 — — RunSqlStructBaseTitle
27
/**//// <summary>
28
/// 运行SQl语句返回结构数组BaseTitle
29
/// </summary>
30
/// <param name="SQL">查询语句。比如select myName from tableName</param>
31
/// <param name="SQLForCount">用于统计记录数的SQL语句。如果传入数字的话,则直接使用数字作为记录数。</param>
32
/// <param name="TitleCount">标题的字符数,一个汉字按照两个字符计算。传入“0”则表示不截取标题。</param>
33
/// <param name="DateFormat">时间格式。比如“yyyy-MM-dd HH:mm:ss dddd”</param>
34
/// <returns>返回BaseTitle结构的数组。URL,标题,时间,人气</returns>
35
public BaseTitle[] RunSqlStructBaseTitle(string SQL,int DataCount,int TitleCount,string DateFormat,int IntroCount)
36
{
37
if (DataCount <1)
38
return null;
39
40
return RunSqlStructBT(SQL,DataCount,TitleCount,DateFormat,IntroCount);
41
}
42
#endregion
43
44
函数实现 — — RunSqlStructBaseTitle#region 函数实现 — — RunSqlStructBaseTitle
45
/**//// <summary>
46
/// 运行SQl语句返回结构数组BaseTitle
47
/// </summary>
48
/// <param name="SQL">查询语句。比如select myName from tableName</param>
49
/// <param name="SQLForCount">用于统计记录数的SQL语句。如果传入数字的话,则直接使用数字作为记录数。</param>
50
/// <param name="TitleCount">标题的字符数,一个汉字按照两个字符计算。传入“0”则表示不截取标题。</param>
51
/// <param name="DateFormat">时间格式。比如“yyyy-MM-dd HH:mm:ss dddd”</param>
52
/// <returns>返回BaseTitle结构的数组。URL,标题,时间,人气</returns>
53
private BaseTitle[] RunSqlStructBT(string SQL,int DataCount,int TitleCount,string DateFormat,int IntroCount)
54
{
55
//返回ID 传入查询语句,返回第一条记录的第一的字段的值
56
SetCommand(SQL,1); //设置command
57
SqlDataReader r = null;
58
try
59
{
60
if (cm.Connection.State == ConnectionState.Broken || cm.Connection.State == ConnectionState.Closed )
61
cm.Connection.Open();
62
63
BaseTitle[] strValue = new BaseTitle[DataCount];
64
r = cm.ExecuteReader();
65
int i = 0;
66
while (r.Read())
67
{
68
//主键
69
strValue[i].ID = r[0].ToString();
70
//网址
71
strValue[i].URL = r[1].ToString();
72
//判断截取字符数
73
if (TitleCount == 0)
74
{
75
strValue[i].AllTitle = r[2].ToString();
76
strValue[i].Title = r[2].ToString();
77
}
78
else
79
{
80
strValue[i].AllTitle = r[2].ToString();
81
strValue[i].Title = Functions.strCal(r[2].ToString(),TitleCount);
82
}
83
84
//判断时间
85
if (DateFormat.Length == 0 )
86
strValue[i].AddedDate = r[3].ToString();
87
else
88
strValue[i].AddedDate = DateTime.Parse(r[3].ToString()).ToString(DateFormat);
89
90
//人气
91
strValue[i].Hits = r[4].ToString();
92
//图片路径
93
strValue[i].ImagePath = r[5].ToString();
94
//内容简介
95
if (IntroCount == 0)
96
strValue[i].Introduction = r[6].ToString();
97
else
98
strValue[i].Introduction = Functions.strCal(r[6].ToString(),IntroCount);
99
100
i++;
101
}
102
103
// if (i == 0)
104
// {
105
// //没有数据,返回空
106
// return null;
107
// }
108
// else if (i < DataCount )
109
// {
110
// //记录数不够用,修改数组大小
111
// BaseTitle[] tmp = new BaseTitle[i];
112
// int j = 0;
113
// foreach(BaseTitle tt in strValue)
114
// {
115
// tmp[j].Hits = tt.Hits ;
116
// tmp[j].ID = tt.ID ;
117
// tmp[j].ImagePath = tt.ImagePath ;
118
// tmp[j].Introduction = tt.Introduction ;
119
// tmp[j].Title = tt.Title ;
120
// tmp[j].URL = tt.URL ;
121
// j++;
122
// if (j == i )
123
// break;
124
// }
125
// return tmp;
126
// }
127
// else
128
// {
129
// return strValue;
130
// }
131
return strValue;
132
}
133
catch(Exception ex)
134
{
135
SetErrorMsg("RunSqlStructBT",SQL,ex.Message ); //处理错误
136
return null;
137
}
138
finally
139
{
140
if (r != null)
141
r.Close();
142
143
if (!isUseTrans)
144
cm.Connection.Close();
145
146
}
147
}
148
#endregion
149
150
//=====================================================================
151
152
函数实现 — — RunSqlStructCusTitle#region 函数实现 — — RunSqlStructCusTitle
153
/**//// <summary>
154
/// 运行SQl语句返回结构数组 CusTitle
155
/// </summary>
156
/// <param name="SQL">查询语句。比如select myName from tableName</param>
157
/// <param name="SQLForCount">用于统计记录数的SQL语句。如果传入数字的话,则直接使用数字作为记录数。</param>
158
/// <param name="TitleCount">标题的字符数,一个汉字按照两个字符计算。传入“0”则表示不截取标题。</param>
159
/// <param name="DateFormat">时间格式。比如“yyyy-MM-dd HH:mm:ss dddd”</param>
160
/// <returns>返回BaseTitle结构的数组。URL,标题,时间,人气</returns>
161
public CusTitle[] RunSqlStructCusTitle(string SQL,string SQLForCount)
162
{
163
string strRowCount = RunSqlGetID(SQLForCount);
164
if (strRowCount == null)
165
return null;
166
167
int DataCount = Int32.Parse(strRowCount);
168
if (DataCount <1)
169
return null;
170
171
return RunSqlStructCT(SQL,DataCount);
172
}
173
#endregion
174
175
函数实现 — — RunSqlStructCusTitle#region 函数实现 — — RunSqlStructCusTitle
176
/**//// <summary>
177
/// 运行SQl语句返回结构数组 CusTitle
178
/// </summary>
179
/// <param name="SQL">查询语句。比如select myName from tableName</param>
180
/// <param name="SQLForCount">用于统计记录数的SQL语句。如果传入数字的话,则直接使用数字作为记录数。</param>
181
/// <param name="TitleCount">标题的字符数,一个汉字按照两个字符计算。传入“0”则表示不截取标题。</param>
182
/// <param name="DateFormat">时间格式。比如“yyyy-MM-dd HH:mm:ss dddd”</param>
183
/// <returns>返回BaseTitle结构的数组。URL,标题,时间,人气</returns>
184
public CusTitle[] RunSqlStructCusTitle(string SQL,int DataCount)
185
{
186
if (DataCount <1)
187
return null;
188
189
return RunSqlStructCT(SQL,DataCount);
190
}
191
#endregion
192
193
函数实现 — — RunSqlStructCT#region 函数实现 — — RunSqlStructCT
194
/**//// <summary>
195
/// 运行SQl语句返回结构数组 CusTitle
196
/// </summary>
197
/// <param name="SQL">查询语句。比如select myName from tableName</param>
198
/// <param name="SQLForCount">用于统计记录数的SQL语句。如果传入数字的话,则直接使用数字作为记录数。</param>
199
/// <param name="TitleCount">标题的字符数,一个汉字按照两个字符计算。传入“0”则表示不截取标题。</param>
200
/// <param name="DateFormat">时间格式。比如“yyyy-MM-dd HH:mm:ss dddd”</param>
201
/// <returns>返回BaseTitle结构的数组。URL,标题,时间,人气</returns>
202
private CusTitle[] RunSqlStructCT(string SQL,int DataCount)
203
{
204
//返回ID 传入查询语句,返回第一条记录的第一的字段的值
205
SetCommand(SQL,1); //设置command
206
SqlDataReader r = null;
207
try
208
{
209
if (cm.Connection.State == ConnectionState.Broken || cm.Connection.State == ConnectionState.Closed )
210
cm.Connection.Open();
211
212
CusTitle[] strValue = new CusTitle[DataCount];
213
r = cm.ExecuteReader();
214
int i = 0;
215
int ArrLength = r.FieldCount-2;
216
while (r.Read())
217
{
218
//ID
219
strValue[i].ID = r[0].ToString();
220
//标题
221
strValue[i].Title = r[1].ToString();
222
223
//其他
224
strValue[i].str = new string[ArrLength];
225
for(int j=0;j<ArrLength;j++)
226
strValue[i].str[j] = r.GetValue(j+2).ToString();
227
228
i++;
229
}
230
return strValue;
231
232
}
233
catch(Exception ex)
234
{
235
SetErrorMsg("RunSqlStrs",SQL,ex.Message ); //处理错误
236
return null;
237
}
238
finally
239
{
240
if (r != null)
241
r.Close();
242
243
if (!isUseTrans)
244
cm.Connection.Close();
245
246
}
247
}
248
#endregion
249
250
//===========================end==============================
251
//新增加的部分,返回结构数组用于绑定控件2
3

函数实现 — — RunSqlStructBaseTitle#region 函数实现 — — RunSqlStructBaseTitle4

/**//// <summary>5
/// 运行SQl语句返回结构数组BaseTitle6
/// </summary>7
/// <param name="SQL">查询语句。比如select myName from tableName</param>8
/// <param name="SQLForCount">用于统计记录数的SQL语句。如果传入数字的话,则直接使用数字作为记录数。</param>9
/// <param name="TitleCount">标题的字符数,一个汉字按照两个字符计算。传入“0”则表示不截取标题。</param>10
/// <param name="DateFormat">时间格式。比如“yyyy-MM-dd HH:mm:ss dddd”</param>11
/// <returns>返回BaseTitle结构的数组。URL,标题,时间,人气</returns>12
public BaseTitle[] RunSqlStructBaseTitle(string SQL,string SQLForCount,int TitleCount,string DateFormat,int IntroCount)13

{14
string strRowCount = RunSqlGetID(SQLForCount);15
if (strRowCount == null)16
return null;17
18
int DataCount = Int32.Parse(strRowCount);19
if (DataCount <1)20
return null;21
22
return RunSqlStructBT(SQL,DataCount,TitleCount,DateFormat,IntroCount);23
}24
#endregion25

26

函数实现 — — RunSqlStructBaseTitle#region 函数实现 — — RunSqlStructBaseTitle27

/**//// <summary>28
/// 运行SQl语句返回结构数组BaseTitle29
/// </summary>30
/// <param name="SQL">查询语句。比如select myName from tableName</param>31
/// <param name="SQLForCount">用于统计记录数的SQL语句。如果传入数字的话,则直接使用数字作为记录数。</param>32
/// <param name="TitleCount">标题的字符数,一个汉字按照两个字符计算。传入“0”则表示不截取标题。</param>33
/// <param name="DateFormat">时间格式。比如“yyyy-MM-dd HH:mm:ss dddd”</param>34
/// <returns>返回BaseTitle结构的数组。URL,标题,时间,人气</returns>35
public BaseTitle[] RunSqlStructBaseTitle(string SQL,int DataCount,int TitleCount,string DateFormat,int IntroCount)36

{37
if (DataCount <1)38
return null;39
40
return RunSqlStructBT(SQL,DataCount,TitleCount,DateFormat,IntroCount);41
}42
#endregion43

44

函数实现 — — RunSqlStructBaseTitle#region 函数实现 — — RunSqlStructBaseTitle45

/**//// <summary>46
/// 运行SQl语句返回结构数组BaseTitle47
/// </summary>48
/// <param name="SQL">查询语句。比如select myName from tableName</param>49
/// <param name="SQLForCount">用于统计记录数的SQL语句。如果传入数字的话,则直接使用数字作为记录数。</param>50
/// <param name="TitleCount">标题的字符数,一个汉字按照两个字符计算。传入“0”则表示不截取标题。</param>51
/// <param name="DateFormat">时间格式。比如“yyyy-MM-dd HH:mm:ss dddd”</param>52
/// <returns>返回BaseTitle结构的数组。URL,标题,时间,人气</returns>53
private BaseTitle[] RunSqlStructBT(string SQL,int DataCount,int TitleCount,string DateFormat,int IntroCount)54

{55
//返回ID 传入查询语句,返回第一条记录的第一的字段的值56
SetCommand(SQL,1); //设置command57
SqlDataReader r = null;58
try59

{60
if (cm.Connection.State == ConnectionState.Broken || cm.Connection.State == ConnectionState.Closed )61
cm.Connection.Open();62
63
BaseTitle[] strValue = new BaseTitle[DataCount];64
r = cm.ExecuteReader();65
int i = 0;66
while (r.Read())67

{68
//主键69
strValue[i].ID = r[0].ToString();70
//网址71
strValue[i].URL = r[1].ToString();72
//判断截取字符数73
if (TitleCount == 0)74

{75
strValue[i].AllTitle = r[2].ToString();76
strValue[i].Title = r[2].ToString();77
}78
else79

{80
strValue[i].AllTitle = r[2].ToString();81
strValue[i].Title = Functions.strCal(r[2].ToString(),TitleCount);82
}83

84
//判断时间85
if (DateFormat.Length == 0 )86
strValue[i].AddedDate = r[3].ToString();87
else88
strValue[i].AddedDate = DateTime.Parse(r[3].ToString()).ToString(DateFormat);89

90
//人气91
strValue[i].Hits = r[4].ToString();92
//图片路径93
strValue[i].ImagePath = r[5].ToString();94
//内容简介95
if (IntroCount == 0)96
strValue[i].Introduction = r[6].ToString();97
else98
strValue[i].Introduction = Functions.strCal(r[6].ToString(),IntroCount);99
100
i++;101
}102

103
// if (i == 0)104
// {105
// //没有数据,返回空106
// return null;107
// }108
// else if (i < DataCount )109
// {110
// //记录数不够用,修改数组大小111
// BaseTitle[] tmp = new BaseTitle[i];112
// int j = 0;113
// foreach(BaseTitle tt in strValue)114
// {115
// tmp[j].Hits = tt.Hits ;116
// tmp[j].ID = tt.ID ;117
// tmp[j].ImagePath = tt.ImagePath ;118
// tmp[j].Introduction = tt.Introduction ;119
// tmp[j].Title = tt.Title ;120
// tmp[j].URL = tt.URL ;121
// j++;122
// if (j == i )123
// break;124
// }125
// return tmp;126
// }127
// else128
// {129
// return strValue;130
// }131
return strValue;132
}133
catch(Exception ex)134

{135
SetErrorMsg("RunSqlStructBT",SQL,ex.Message ); //处理错误136
return null;137
}138
finally139

{140
if (r != null)141
r.Close();142
143
if (!isUseTrans)144
cm.Connection.Close();145
146
}147
}148
#endregion149

150
//=====================================================================151

152

函数实现 — — RunSqlStructCusTitle#region 函数实现 — — RunSqlStructCusTitle153

/**//// <summary>154
/// 运行SQl语句返回结构数组 CusTitle155
/// </summary>156
/// <param name="SQL">查询语句。比如select myName from tableName</param>157
/// <param name="SQLForCount">用于统计记录数的SQL语句。如果传入数字的话,则直接使用数字作为记录数。</param>158
/// <param name="TitleCount">标题的字符数,一个汉字按照两个字符计算。传入“0”则表示不截取标题。</param>159
/// <param name="DateFormat">时间格式。比如“yyyy-MM-dd HH:mm:ss dddd”</param>160
/// <returns>返回BaseTitle结构的数组。URL,标题,时间,人气</returns>161
public CusTitle[] RunSqlStructCusTitle(string SQL,string SQLForCount)162

{163
string strRowCount = RunSqlGetID(SQLForCount);164
if (strRowCount == null)165
return null;166
167
int DataCount = Int32.Parse(strRowCount);168
if (DataCount <1)169
return null;170
171
return RunSqlStructCT(SQL,DataCount);172
}173
#endregion174

175

函数实现 — — RunSqlStructCusTitle#region 函数实现 — — RunSqlStructCusTitle176

/**//// <summary>177
/// 运行SQl语句返回结构数组 CusTitle178
/// </summary>179
/// <param name="SQL">查询语句。比如select myName from tableName</param>180
/// <param name="SQLForCount">用于统计记录数的SQL语句。如果传入数字的话,则直接使用数字作为记录数。</param>181
/// <param name="TitleCount">标题的字符数,一个汉字按照两个字符计算。传入“0”则表示不截取标题。</param>182
/// <param name="DateFormat">时间格式。比如“yyyy-MM-dd HH:mm:ss dddd”</param>183
/// <returns>返回BaseTitle结构的数组。URL,标题,时间,人气</returns>184
public CusTitle[] RunSqlStructCusTitle(string SQL,int DataCount)185

{186
if (DataCount <1)187
return null;188
189
return RunSqlStructCT(SQL,DataCount);190
}191
#endregion192
193

函数实现 — — RunSqlStructCT#region 函数实现 — — RunSqlStructCT194

/**//// <summary>195
/// 运行SQl语句返回结构数组 CusTitle196
/// </summary>197
/// <param name="SQL">查询语句。比如select myName from tableName</param>198
/// <param name="SQLForCount">用于统计记录数的SQL语句。如果传入数字的话,则直接使用数字作为记录数。</param>199
/// <param name="TitleCount">标题的字符数,一个汉字按照两个字符计算。传入“0”则表示不截取标题。</param>200
/// <param name="DateFormat">时间格式。比如“yyyy-MM-dd HH:mm:ss dddd”</param>201
/// <returns>返回BaseTitle结构的数组。URL,标题,时间,人气</returns>202
private CusTitle[] RunSqlStructCT(string SQL,int DataCount)203

{204
//返回ID 传入查询语句,返回第一条记录的第一的字段的值205
SetCommand(SQL,1); //设置command206
SqlDataReader r = null;207
try208

{209
if (cm.Connection.State == ConnectionState.Broken || cm.Connection.State == ConnectionState.Closed )210
cm.Connection.Open();211
212
CusTitle[] strValue = new CusTitle[DataCount];213
r = cm.ExecuteReader();214
int i = 0;215
int ArrLength = r.FieldCount-2;216
while (r.Read())217

{218
//ID219
strValue[i].ID = r[0].ToString();220
//标题221
strValue[i].Title = r[1].ToString();222
223
//其他224
strValue[i].str = new string[ArrLength];225
for(int j=0;j<ArrLength;j++)226
strValue[i].str[j] = r.GetValue(j+2).ToString();227

228
i++;229
}230
return strValue;231
232
}233
catch(Exception ex)234

{235
SetErrorMsg("RunSqlStrs",SQL,ex.Message ); //处理错误236
return null;237
}238
finally239

{240
if (r != null)241
r.Close();242
243
if (!isUseTrans)244
cm.Connection.Close();245
246
}247
}248
#endregion249

250
//===========================end==============================251

下载全部源文件。