温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:某市人口普查系统源码
当前文件路径:RenKouPuCha/App_Code/CCUtility.cs

1using 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.Collections; 11
using System.ComponentModel; 12
using System.Drawing; 13
using System.Web.SessionState; 14
using System.Data.SqlClient; 15
/// <summary> 16
/// CCUtility 的摘要说明 17
/// </summary> 18
//该源码下载自www.51aspx.com(51aspx.com) 19
20
public class CCUtility 21
{ 22
/// <summary> 23
/// CCUtility 类文件中,包含了一系列的数据操作,例如连接数据库,获取数据库,绑定数据,关闭连接等 24
/// </summary> 25
26
//全局变量 27
public const int FIELD_TYPE_Text = 0; 28
public const int FIELD_TYPE_Number = 1; 29
public const int FIELD_TYPE_Date = 2; 30
public const int FIELD_TYPE_Memo = 3; 31
protected HttpSessionState Session; 32
protected HttpServerUtility Server; 33
protected HttpRequest Request; 34
protected HttpResponse Response; 35
public DataSet ds; 36
37
public CCUtility() 38
{ 39
ds = new DataSet(); 40
} 41
public DataSet DS 42
{ 43
get 44
{ 45
return ds; 46
} 47
} 48
49
//根据itype的值,将字符串Param中的一些字符用指定的字符替换,使其成为真正的SQL字符串,从而实现数据库连接 50
public static string ToSQL(string Param, int iType) 51
{ 52
if (Param == null || Param.Length == 0) 53
{ 54
return "null"; 55
} 56
else 57
{ 58
string str = Quote(Param); 59
if (iType == FIELD_TYPE_Number) 60
{ 61
return str.Replace(',', '.'); 62
} 63
else 64
{ 65
return "\'" + str + "\'"; 66
} 67
} 68
} 69
70
//用于验证value是否为数值型,如果是,返回true,否则返回false 71
public bool IsNumeric(object source, string value) 72
{ 73
try 74
{ 75
Decimal temp = Convert.ToDecimal(value); 76
return true; 77
} 78
catch 79
{ 80
return false; 81
} 82
} 83
84
//首先验证字符串param是否为空,如果非空,就将字符串Param中的单引号替换为双引号 85
public static string Quote(string Param) 86
{ 87
if (Param == null || Param.Length == 0) 88
{ 89
return ""; 90
} 91
else 92
{ 93
return Param.Replace("'", "''"); 94
} 95
} 96
97
//用于获得Datarow对象row中的field值 98
public static string GetValue(DataRow row, string field) 99
{ 100
if (row[field].ToString() == null) 101
return ""; 102
else 103
return row[field].ToString(); 104
} 105
106
//下面代码首先定义了一个全局变量,然后建立了一个Dataset数据集,最后用sqldataadapter方法与数据库建立了数据连接 107
public SqlConnection Connection; 108
//建立一个数据集 109
public DataSet FillDataSet(string sSQL) 110
{ 111
DataSet ds = new DataSet(); 112
SqlDataAdapter command = new SqlDataAdapter(sSQL, Connection); 113
return ds; 114
} 115
116
//用于向数据集中填充数据,并返回该数据集 117
public int FillDataSet(string sSQL, ref DataSet ds) 118
{ 119
SqlDataAdapter command = new SqlDataAdapter(sSQL, Connection); 120
return command.Fill(ds, "Table"); 121
} 122
123
//用于在Dataset数据集中添加或刷新行 124
public int FillDataSet(string sSQL, ref DataSet ds, int start, int count) 125
{ 126
SqlDataAdapter command = new SqlDataAdapter(sSQL, Connection); 127
return command.Fill(ds, start, count, "Table"); 128
} 129
130
//用于打开数据库连接 131
public void DBOpen() 132
{ 133
string connectionString = "server=(local);database=Censuse;uid=sa;pwd=sa;"; 134
Connection = new SqlConnection(connectionString); 135
Connection.Open(); 136
137
138
} 139
140
141
//用close方法断开与数据库的连接 142
public void DBClose() 143
{ 144
Connection.Close(); 145
} 146
147
////定义了获取页面传递参数的方法,可以用request的querystring或form方法获取传递的参数: 148
////querystring方法用于不同页面间参数传递的获取,form方法用于同一页面中的参数获取 149
//public string GetParam(string ParamName) 150
//{ 151
// string Param = Request.QueryString[ParamName]; 152
// if (Param == null) 153
// Param = Request.Form[ParamName]; 154
// if (Param == null) 155
// return ""; 156
// else 157
// return Param; 158
//} 159
160
////定义了数据库查询,并将字符串数据结果返回的方法。在此用select语句查询数据,然后创建了一个SqlCommand对象执行此 161
////SQL语句,接下来用Executereader方法获取数据,最后用close方法关闭SqlDataReade对象。 162
//public string Dlookup(string table, string field, string sWhere) 163
//{ 164
// string sSQL = "SELECT " + field + " FROM " + table + " WHERE " + sWhere; 165
// SqlCommand command = new SqlCommand(sSQL, Connection); 166
// SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow); 167
// string sReturn; 168
// if (reader.Read()) 169
// { 170
// sReturn = reader[0].ToString(); 171
// if (sReturn == null) 172
// sReturn = ""; 173
// } 174
// else 175
// { 176
// sReturn = ""; 177
// } 178
// reader.Close(); 179
// return sReturn; 180
//} 181
182
////实现了数据库查询,并返回了数值型数据 183
//public int DlookupInt(string table, string field, string sWhere) 184
//{ 185
// string sSQL = "SELECT " + field + " FROM " + table + " where " + sWhere; 186
// SqlCommand command = new SqlCommand(sSQL, Connection); 187
// SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow); 188
// int iReturn = -1; 189
// if (reader.Read()) 190
// { 191
// iReturn = reader.GetInt32(0); 192
// } 193
// reader.Close(); 194
// return iReturn; 195
//} 196
197
////执行语句 198
//public void Execute(string sSQL) 199
//{ 200
// SqlCommand cmd = new SqlCommand(sSQL, Connection); 201
// cmd.ExecuteNonQuery(); 202
//} 203
204
////下面的代码使用了ExecuteReader()方法将数据查询的返回值添加到Items中,并将数据绑定到ListBox控件上。在public ICollection 205
//// buildlistbox事件中,建立了一个新数据集,在次数据集上建立了一个表lookup,并且为此表添加字段值,然后将结果值返回。 206
//public void buildListBox(ListItemCollection Items, string sSQL, string sId, string sTitle, 207
// string CustomInitialDisplayValue, string CustomInitialSubmitValue) 208
//{ 209
// Items.Clear(); 210
// SqlCommand command = new SqlCommand(sSQL, Connection); 211
// SqlDataReader reader = command.ExecuteReader(); 212
// if (CustomInitialDisplayValue != null) 213
// Items.Add(new ListItem(CustomInitialDisplayValue, CustomInitialSubmitValue)); 214
// while (reader.Read()) 215
// { 216
// if (sId == "" && sTitle == "") 217
// { 218
// Items.Add(new ListItem(reader[1].ToString(), reader[0].ToString())); 219
// } 220
// else 221
// { 222
// Items.Add(new ListItem(reader[sTitle].ToString(), reader[sId].ToString())); 223
// } 224
// } 225
// reader.Close(); 226
//} 227
//下面的代码是当有级联下拉菜单时,上级菜单选项发生变化,影响下级菜单的选项 228
public void SelectedItemChanged(ref DropDownList ddl,ref DataSet ds,string sql, string tableName, string textField, string valueField) 229
{ 230
ddl.Items.Clear(); 231
DBOpen(); 232
SqlCommand cmd = new SqlCommand(sql, Connection); 233
SqlDataAdapter da = new SqlDataAdapter(); 234
da.SelectCommand = cmd; 235
da.Fill(ds, tableName); 236
DBClose(); 237
ddl.DataSource = ds.Tables[tableName].DefaultView; 238
ddl.DataTextField = textField; 239
ddl.DataValueField = valueField; 240
ddl.DataBind(); 241
} 242
} 243







