温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:达达ASP.NET企业信息管理系统
当前文件路径:DaDaEnterprise/App_Code/DataAccess.cs

1using System; 2
using System.Data; 3
using System.Data.OleDb; 4
using System.Configuration; 5
using System.Web; 6
using System.Web.Security; 7
using System.Web.UI; 8
using System.Web.UI.HtmlControls; 9
using System.Web.UI.WebControls; 10
using System.Web.UI.WebControls.WebParts; 11
//作者:宇宙达,上传者:yz7805325 12
//源码下载及讨论地址:http://www.51aspx.com/CV/DaDaEnterprise 13
/// <summary> 14
/// 数据类 15
/// </summary> 16
public class DataAccess 17
{ 18
private static OleDbConnection Conn = new OleDbConnection(); 19
private static OleDbCommand Cmd = new OleDbCommand(); 20
21
public DataAccess() 22
{ 23
24
} 25
26
/// <summary> 27
/// 打开连接 28
/// </summary> 29
public static void Open() 30
{ 31
if (Conn.State.Equals(ConnectionState.Closed)) 32
{ 33
Conn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" 34
+ System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.ConnectionStrings["ConnStr"].ToString()); 35
Cmd.Connection = Conn; 36
Conn.Open(); 37
} 38
} 39
40
/// <summary> 41
/// 关闭连接 42
/// </summary> 43
public static void Close() 44
{ 45
if (Conn.State.Equals(ConnectionState.Open)) 46
Conn.Close(); 47
Conn.Dispose(); 48
} 49
50
51
public static DataSet GetDataSet(string sql) 52
{ 53
try 54
{ 55
Open(); 56
OleDbDataAdapter Da = new OleDbDataAdapter(sql, Conn); 57
DataSet Ds = new DataSet(); 58
Da.Fill(Ds); 59
return Ds; 60
} 61
catch (Exception e) 62
{ 63
throw new Exception(e.Message,e); 64
} 65
finally 66
{ 67
Close(); 68
} 69
} 70
71
public static DataTable GetDataTable(string sql) 72
{ 73
try 74
{ 75
Open(); 76
OleDbDataAdapter Da = new OleDbDataAdapter(sql, Conn); 77
DataTable Dt = new DataTable(); 78
Da.Fill(Dt); 79
return Dt; 80
} 81
catch (Exception e) 82
{ 83
throw new Exception(e.Message, e); 84
} 85
finally 86
{ 87
Close(); 88
} 89
} 90
91
public static int ExecuteCmd(string sql) 92
{ 93
try 94
{ 95
Open(); 96
Cmd.CommandType = CommandType.Text; 97
Cmd.CommandText = sql; 98
Cmd.ExecuteNonQuery(); 99
return 1; 100
} 101
catch 102
{ 103
return -1; 104
} 105
finally 106
{ 107
Close(); 108
} 109
} 110
111
/// <summary> 112
/// 统计记录条数 113
/// </summary> 114
/// <param name="sql"></param> 115
/// <returns></returns> 116
public static int CountCmd(string sql) 117
{ 118
try 119
{ 120
Open(); 121
Cmd.CommandType = CommandType.Text; 122
Cmd.CommandText = sql; 123
return (int)Cmd.ExecuteScalar(); 124
} 125
catch (Exception e) 126
{ 127
throw new Exception(e.Message, e); 128
} 129
finally 130
{ 131
Close(); 132
} 133
} 134
135
/// <summary> 136
/// 判断管理员 137
/// </summary> 138
public static void IsAdmin() 139
{ 140
if (HttpContext.Current.Session["Admin"] == null && HttpContext.Current.Session["Pwd"] == null) 141
{ 142
HttpContext.Current.Response.Write(@"<script>alert('你没有登陆或登陆超时!');location.href='./Default.aspx';</script>"); 143
} 144
} 145
} 146







}