温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:苹果投票系统源码及毕业论文
当前文件:
AppleVote/App_Code/DB.cs[2K,2009-6-12 11:31:41],打开代码结构图
AppleVote/App_Code/DB.cs[2K,2009-6-12 11:31:41],打开代码结构图12
using System; 3
using System.Data; 4
using System.Configuration; 5
using System.Web; 6
using System.Web.Security; 7
using System.Web.UI; 8
using System.Web.UI.WebControls; 9
using System.Web.UI.WebControls.WebParts; 10
using System.Web.UI.HtmlControls; 11
using System.Data.SqlClient; 12
13
/// <summary> 14
/// data 的摘要说明 15
/// </summary> 16
public class DB 17
{ 18
SqlConnection con = null;//定义一个类属性,类型为SQL连接 19
SqlCommand cmd = new SqlCommand();//针对数据库操作的命令对象 20
21
public DB() 22
{ 23
con = new SqlConnection("Data Source=.;Initial Catalog=vote;uid=sa;pwd=sa;"); 24
} 25
public SqlConnection getcon() 26
{ 27
if (con.State == ConnectionState.Closed) 28
con.Open(); 29
return con; 30
31
32
} 33
public SqlConnection clear() 34
{ 35
if (con.State == ConnectionState.Open) 36
con.Close(); 37
return con; 38
39
40
41
} 42
public void NonQueryexecute(string strsql) 43
{ 44
45
cmd.Connection = getcon(); 46
cmd.CommandText = strsql; 47
cmd.ExecuteNonQuery(); 48
clear(); 49
50
51
} 52
//5 1 a s p x.com 53
54
public string executeGetReturn(string strsql)//strsql是传入SQL命令语句 55
{ 56
//getcon(); 57
cmd.Connection = getcon(); 58
cmd.CommandText = strsql; 59
string result = cmd.ExecuteScalar().ToString(); 60
clear(); 61
return result; 62
63
} 64
public SqlDataReader getsdr(string strsql) 65
{ 66
cmd.Connection = getcon(); 67
cmd.CommandText = strsql; 68
SqlDataReader sdr = cmd.ExecuteReader(); 69
return sdr; 70
71
72
} 73
public bool trantion(string[] sql) 74
{ 75
bool flag = false; 76
con = getcon(); 77
cmd.Connection = con; 78
SqlTransaction mytransaction; 79
mytransaction = con.BeginTransaction(); 80
try 81
{ 82
for (int i = 0; i < sql.Length; i++) 83
{ 84
cmd.Transaction = mytransaction; 85
cmd.CommandText = sql[i]; 86
cmd.ExecuteNonQuery(); 87
88
} 89
mytransaction.Commit(); 90
flag = true; 91
92
} 93
catch 94
{ 95
mytransaction.Rollback(); 96
flag = false; 97
98
} 99
finally 100
{ 101
clear(); 102
103
} 104
105
return flag; 106
107
108
} 109
} 110








}