代码
1 /**//// <summary>
2 /// 数据访问基础类(基于MySQL)
3 /// Copyright (C) 2004-2008 By HOMEZZM
4 /// </summary>
5 public abstract class DbHelperMySQL
6 {
7 //数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
8 public static string connectionString = PubConstant.ConnectionString;
9 public DbHelperMySQL()
10 {
11 }
12
13 公用方法#region 公用方法
14
15 public static int GetMaxID(string FieldName, string TableName)
16 {
17 string strsql = "select max(" + FieldName + ")+1 from " + TableName;
18 object obj = GetSingle(strsql);
19 if (obj == null)
20 {
21 return 1;
22 }
23 else
24 {
25 return int.Parse(obj.ToString());
26 }
27 }
28 public static bool Exists(string strSql)
29 {
30 object obj = GetSingle(strSql);
31 int cmdresult;
32 if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
33 {
34 cmdresult = 0;
35 }
36 else
37 {
38 cmdresult = int.Parse(obj.ToString());
39 }
40 if (cmdresult == 0)
41 {
42 return false;
43 }
44 else
45 {
46 return true;
47 }
48 }
49 public static bool Exists(string strSql, params MySqlParameter[] cmdParms)
50 {
51 object obj = GetSingle(strSql, cmdParms);
52 int cmdresult;
53 if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
54 {
55 cmdresult = 0;
56 }
57 else
58 {
59 cmdresult = int.Parse(obj.ToString());
60 }
61 if (cmdresult == 0)
62 {
63 return false;
64 }
65 else
66 {
67 return true;
68 }
69 }
70 #endregion
71
72 执行简单SQL语句#region 执行简单SQL语句
73
74 /**//// <summary>
75 /// 执行SQL语句,返回影响的记录数
76 /// </summary>
77 /// <param name="SQLString">SQL语句</param>
78 /// <returns>影响的记录数</returns>
79 public static int ExecuteSql(string SQLString)
80 {
81 using (MySqlConnection connection = new MySqlConnection(connectionString))
82 {
83 using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
84 {
85 try
86 {
87 connection.Open();
88 int rows = cmd.ExecuteNonQuery();
89 return rows;
90 }
91 catch (MySql.Data.MySqlClient.MySqlException e)
92 {
93 connection.Close();
94 throw e;
95 }
96 }
97 }
98 }
99
100 public static int ExecuteSqlByTime(string SQLString, int Times)
101 {
102 using (MySqlConnection connection = new MySqlConnection(connectionString))
103 {
104 using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
105 {
106 try
107 {
108 connection.Open();
109 cmd.CommandTimeout = Times;
110 int rows = cmd.ExecuteNonQuery();
111 return rows;
112 }
113 catch (MySql.Data.MySqlClient.MySqlException e)
114 {
115 connection.Close();
116 throw e;
117 }
118 }
119 }
120 }
121
122 /**//// <summary>
123 /// 执行Sql和Oracle滴混合事务
124 /// </summary>
125 /// <param name="list">SQL命令行列表</param>
126 /// <param name="oracleCmdSqlList">Oracle命令行列表</param>
127 /// <returns>执行结果 0-由于SQL造成事务失败 -1 由于Oracle造成事务失败 1-整体事务执行成功</returns>
128 public static int ExecuteSqlTran(List<CommandInfo> list, List<CommandInfo> oracleCmdSqlList)
129 {
130 using (MySqlConnection conn = new MySqlConnection(connectionString))
131 {
132 conn.Open();
133 MySqlCommand cmd = new MySqlCommand();
134 cmd.Connection = conn;
135 MySqlTransaction tx = conn.BeginTransaction();
136 cmd.Transaction = tx;
137 try
138 {
139 foreach (CommandInfo myDE in list)
140 {
141 string cmdText = myDE.CommandText;
142 MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Parameters;
143 PrepareCommand(cmd, conn, tx, cmdText, cmdParms);
144 if (myDE.EffentNextType == EffentNextType.SolicitationEvent)
145
2 /// 数据访问基础类(基于MySQL)
3 /// Copyright (C) 2004-2008 By HOMEZZM
4 /// </summary>
5 public abstract class DbHelperMySQL
6 {
7 //数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
8 public static string connectionString = PubConstant.ConnectionString;
9 public DbHelperMySQL()
10 {
11 }
12
13 公用方法#region 公用方法
14
15 public static int GetMaxID(string FieldName, string TableName)
16 {
17 string strsql = "select max(" + FieldName + ")+1 from " + TableName;
18 object obj = GetSingle(strsql);
19 if (obj == null)
20 {
21 return 1;
22 }
23 else
24 {
25 return int.Parse(obj.ToString());
26 }
27 }
28 public static bool Exists(string strSql)
29 {
30 object obj = GetSingle(strSql);
31 int cmdresult;
32 if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
33 {
34 cmdresult = 0;
35 }
36 else
37 {
38 cmdresult = int.Parse(obj.ToString());
39 }
40 if (cmdresult == 0)
41 {
42 return false;
43 }
44 else
45 {
46 return true;
47 }
48 }
49 public static bool Exists(string strSql, params MySqlParameter[] cmdParms)
50 {
51 object obj = GetSingle(strSql, cmdParms);
52 int cmdresult;
53 if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
54 {
55 cmdresult = 0;
56 }
57 else
58 {
59 cmdresult = int.Parse(obj.ToString());
60 }
61 if (cmdresult == 0)
62 {
63 return false;
64 }
65 else
66 {
67 return true;
68 }
69 }
70 #endregion
71
72 执行简单SQL语句#region 执行简单SQL语句
73
74 /**//// <summary>
75 /// 执行SQL语句,返回影响的记录数
76 /// </summary>
77 /// <param name="SQLString">SQL语句</param>
78 /// <returns>影响的记录数</returns>
79 public static int ExecuteSql(string SQLString)
80 {
81 using (MySqlConnection connection = new MySqlConnection(connectionString))
82 {
83 using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
84 {
85 try
86 {
87 connection.Open();
88 int rows = cmd.ExecuteNonQuery();
89 return rows;
90 }
91 catch (MySql.Data.MySqlClient.MySqlException e)
92 {
93 connection.Close();
94 throw e;
95 }
96 }
97 }
98 }
99
100 public static int ExecuteSqlByTime(string SQLString, int Times)
101 {
102 using (MySqlConnection connection = new MySqlConnection(connectionString))
103 {
104 using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
105 {
106 try
107 {
108 connection.Open();
109 cmd.CommandTimeout = Times;
110 int rows = cmd.ExecuteNonQuery();
111 return rows;
112 }
113 catch (MySql.Data.MySqlClient.MySqlException e)
114 {
115 connection.Close();
116 throw e;
117 }
118 }
119 }
120 }
121
122 /**//// <summary>
123 /// 执行Sql和Oracle滴混合事务
124 /// </summary>
125 /// <param name="list">SQL命令行列表</param>
126 /// <param name="oracleCmdSqlList">Oracle命令行列表</param>
127 /// <returns>执行结果 0-由于SQL造成事务失败 -1 由于Oracle造成事务失败 1-整体事务执行成功</returns>
128 public static int ExecuteSqlTran(List<CommandInfo> list, List<CommandInfo> oracleCmdSqlList)
129 {
130 using (MySqlConnection conn = new MySqlConnection(connectionString))
131 {
132 conn.Open();
133 MySqlCommand cmd = new MySqlCommand();
134 cmd.Connection = conn;
135 MySqlTransaction tx = conn.BeginTransaction();
136 cmd.Transaction = tx;
137 try
138 {
139 foreach (CommandInfo myDE in list)
140 {
141 string cmdText = myDE.CommandText;
142 MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Parameters;
143 PrepareCommand(cmd, conn, tx, cmdText, cmdParms);
144 if (myDE.EffentNextType == EffentNextType.SolicitationEvent)
145
作者: bearer
- 该日志由 bearer 于12年前发表在综合分类下,最后更新于 2012年03月27日.
- 转载请注明: NET-数据访问基础类(基于MySQL) | 学步园 +复制链接
抱歉!评论已关闭.