123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660 |
- using MySql.Data.MySqlClient;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TimedUpload
- {
- class MysqlHelper
- {
-
- private static string connectionString = ConfigurationManager.AppSettings["dbMysql"];
-
- #region 公用方法
-
-
-
-
-
-
- public static int GetMaxID(string FieldName, string TableName)
- {
- string strsql = "select max(" + FieldName + ")+1 from " + TableName;
- object obj = GetSingle(strsql);
- if (obj == null)
- {
- return 1;
- }
- else
- {
- return int.Parse(obj.ToString());
- }
- }
-
-
-
-
-
- public static bool Exists(string strSql)
- {
- object obj = GetSingle(strSql);
- int cmdresult;
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- cmdresult = 0;
- }
- else
- {
- cmdresult = int.Parse(obj.ToString());
- }
- if (cmdresult == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
-
-
-
-
-
-
- public static bool Exists(string strSql, params MySqlParameter[] cmdParms)
- {
- object obj = GetSingle(strSql, cmdParms);
- int cmdresult;
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- cmdresult = 0;
- }
- else
- {
- cmdresult = int.Parse(obj.ToString());
- }
- if (cmdresult == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- #endregion
- #region 执行简单SQL语句
-
-
-
-
-
- public static int ExecuteSql(string SQLString)
- {
- using (MySqlConnection connection = new MySqlConnection(connectionString))
- {
- using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
- {
- try
- {
- connection.Open();
- int rows = cmd.ExecuteNonQuery();
- return rows;
- }
- catch (MySql.Data.MySqlClient.MySqlException e)
- {
- connection.Close();
- throw e;
- }
- }
- }
- }
- public static int ExecuteSqlByTime(string SQLString, int Times)
- {
- using (MySqlConnection connection = new MySqlConnection(connectionString))
- {
- using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
- {
- try
- {
- connection.Open();
- cmd.CommandTimeout = Times;
- int rows = cmd.ExecuteNonQuery();
- return rows;
- }
- catch (MySql.Data.MySqlClient.MySqlException e)
- {
- connection.Close();
- throw e;
- }
- }
- }
- }
-
-
-
-
- public static int ExecuteSqlTran(List<String> SQLStringList)
- {
- using (MySqlConnection conn = new MySqlConnection(connectionString))
- {
- conn.Open();
- MySqlCommand cmd = new MySqlCommand();
- cmd.Connection = conn;
- MySqlTransaction tx = conn.BeginTransaction();
- cmd.Transaction = tx;
- try
- {
- int count = 0;
- for (int n = 0; n < SQLStringList.Count; n++)
- {
- string strsql = SQLStringList[n];
- if (strsql.Trim().Length > 1)
- {
- cmd.CommandText = strsql;
- count += cmd.ExecuteNonQuery();
- }
- }
- tx.Commit();
- return count;
- }
- catch
- {
- tx.Rollback();
- return 0;
- }
- }
- }
-
-
-
-
-
-
- public static int ExecuteSql(string SQLString, string content)
- {
- using (MySqlConnection connection = new MySqlConnection(connectionString))
- {
- MySqlCommand cmd = new MySqlCommand(SQLString, connection);
- MySql.Data.MySqlClient.MySqlParameter myParameter = new MySql.Data.MySqlClient.MySqlParameter("@content", SqlDbType.NText);
- myParameter.Value = content;
- cmd.Parameters.Add(myParameter);
- try
- {
- connection.Open();
- int rows = cmd.ExecuteNonQuery();
- return rows;
- }
- catch (MySql.Data.MySqlClient.MySqlException e)
- {
- throw e;
- }
- finally
- {
- cmd.Dispose();
- connection.Close();
- }
- }
- }
-
-
-
-
-
-
- public static object ExecuteSqlGet(string SQLString, string content)
- {
- using (MySqlConnection connection = new MySqlConnection(connectionString))
- {
- MySqlCommand cmd = new MySqlCommand(SQLString, connection);
- MySql.Data.MySqlClient.MySqlParameter myParameter = new MySql.Data.MySqlClient.MySqlParameter("@content", SqlDbType.NText);
- myParameter.Value = content;
- cmd.Parameters.Add(myParameter);
- try
- {
- connection.Open();
- object obj = cmd.ExecuteScalar();
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- return null;
- }
- else
- {
- return obj;
- }
- }
- catch (MySql.Data.MySqlClient.MySqlException e)
- {
- throw e;
- }
- finally
- {
- cmd.Dispose();
- connection.Close();
- }
- }
- }
-
-
-
-
-
-
- public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
- {
- using (MySqlConnection connection = new MySqlConnection(connectionString))
- {
- MySqlCommand cmd = new MySqlCommand(strSQL, connection);
- MySql.Data.MySqlClient.MySqlParameter myParameter = new MySql.Data.MySqlClient.MySqlParameter("@fs", SqlDbType.Image);
- myParameter.Value = fs;
- cmd.Parameters.Add(myParameter);
- try
- {
- connection.Open();
- int rows = cmd.ExecuteNonQuery();
- return rows;
- }
- catch (MySql.Data.MySqlClient.MySqlException e)
- {
- throw e;
- }
- finally
- {
- cmd.Dispose();
- connection.Close();
- }
- }
- }
-
-
-
-
-
- public static object GetSingle(string SQLString)
- {
- using (MySqlConnection connection = new MySqlConnection(connectionString))
- {
- using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
- {
- try
- {
- connection.Open();
- object obj = cmd.ExecuteScalar();
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- return null;
- }
- else
- {
- return obj;
- }
- }
- catch (MySql.Data.MySqlClient.MySqlException e)
- {
- connection.Close();
- throw e;
- }
- }
- }
- }
- public static object GetSingle(string SQLString, int Times)
- {
- using (MySqlConnection connection = new MySqlConnection(connectionString))
- {
- using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
- {
- try
- {
- connection.Open();
- cmd.CommandTimeout = Times;
- object obj = cmd.ExecuteScalar();
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- return null;
- }
- else
- {
- return obj;
- }
- }
- catch (MySql.Data.MySqlClient.MySqlException e)
- {
- connection.Close();
- throw e;
- }
- }
- }
- }
-
-
-
-
-
- public static MySqlDataReader ExecuteReader(string strSQL)
- {
- MySqlConnection connection = new MySqlConnection(connectionString);
- MySqlCommand cmd = new MySqlCommand(strSQL, connection);
- try
- {
- connection.Open();
- MySqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- return myReader;
- }
- catch (MySql.Data.MySqlClient.MySqlException e)
- {
- throw e;
- }
- }
-
-
-
-
-
- public static DataSet Query(string SQLString)
- {
- using (MySqlConnection connection = new MySqlConnection(connectionString))
- {
- DataSet ds = new DataSet();
- try
- {
- connection.Open();
- MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
- command.Fill(ds, "ds");
- }
- catch (MySql.Data.MySqlClient.MySqlException ex)
- {
- throw new Exception(ex.Message);
- }
- return ds;
- }
- }
- public static DataSet Query(string SQLString, int Times)
- {
- using (MySqlConnection connection = new MySqlConnection(connectionString))
- {
- DataSet ds = new DataSet();
- try
- {
- connection.Open();
- MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
- command.SelectCommand.CommandTimeout = Times;
- command.Fill(ds, "ds");
- }
- catch (MySql.Data.MySqlClient.MySqlException ex)
- {
- throw new Exception(ex.Message);
- }
- return ds;
- }
- }
- #endregion
- #region 执行带参数的SQL语句
-
-
-
-
-
- public static int ExecuteSql(string SQLString, params MySqlParameter[] cmdParms)
- {
- using (MySqlConnection connection = new MySqlConnection(connectionString))
- {
- using (MySqlCommand cmd = new MySqlCommand())
- {
- try
- {
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- int rows = cmd.ExecuteNonQuery();
- cmd.Parameters.Clear();
- return rows;
- }
- catch (MySql.Data.MySqlClient.MySqlException e)
- {
- throw e;
- }
- }
- }
- }
-
-
-
-
- public static void ExecuteSqlTran(Hashtable SQLStringList)
- {
- using (MySqlConnection conn = new MySqlConnection(connectionString))
- {
- conn.Open();
- using (MySqlTransaction trans = conn.BeginTransaction())
- {
- MySqlCommand cmd = new MySqlCommand();
- try
- {
-
- foreach (DictionaryEntry myDE in SQLStringList)
- {
- string cmdText = myDE.Key.ToString();
- MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Value;
- PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
- int val = cmd.ExecuteNonQuery();
- cmd.Parameters.Clear();
- }
- trans.Commit();
- }
- catch
- {
- trans.Rollback();
- throw;
- }
- }
- }
- }
-
-
-
-
- public static void ExecuteSqlTranWithIndentity(Hashtable SQLStringList)
- {
- using (MySqlConnection conn = new MySqlConnection(connectionString))
- {
- conn.Open();
- using (MySqlTransaction trans = conn.BeginTransaction())
- {
- MySqlCommand cmd = new MySqlCommand();
- try
- {
- int indentity = 0;
-
- foreach (DictionaryEntry myDE in SQLStringList)
- {
- string cmdText = myDE.Key.ToString();
- MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Value;
- foreach (MySqlParameter q in cmdParms)
- {
- if (q.Direction == ParameterDirection.InputOutput)
- {
- q.Value = indentity;
- }
- }
- PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
- int val = cmd.ExecuteNonQuery();
- foreach (MySqlParameter q in cmdParms)
- {
- if (q.Direction == ParameterDirection.Output)
- {
- indentity = Convert.ToInt32(q.Value);
- }
- }
- cmd.Parameters.Clear();
- }
- trans.Commit();
- }
- catch
- {
- trans.Rollback();
- throw;
- }
- }
- }
- }
-
-
-
-
-
- public static object GetSingle(string SQLString, params MySqlParameter[] cmdParms)
- {
- using (MySqlConnection connection = new MySqlConnection(connectionString))
- {
- using (MySqlCommand cmd = new MySqlCommand())
- {
- try
- {
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- object obj = cmd.ExecuteScalar();
- cmd.Parameters.Clear();
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- return null;
- }
- else
- {
- return obj;
- }
- }
- catch (MySql.Data.MySqlClient.MySqlException e)
- {
- throw e;
- }
- }
- }
- }
-
-
-
-
-
- public static MySqlDataReader ExecuteReader(string SQLString, params MySqlParameter[] cmdParms)
- {
- MySqlConnection connection = new MySqlConnection(connectionString);
- MySqlCommand cmd = new MySqlCommand();
- try
- {
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- MySqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- cmd.Parameters.Clear();
- return myReader;
- }
- catch (MySql.Data.MySqlClient.MySqlException e)
- {
- throw e;
- }
-
-
-
-
-
- }
-
-
-
-
-
- public static DataSet Query(string SQLString, params MySqlParameter[] cmdParms)
- {
- using (MySqlConnection connection = new MySqlConnection(connectionString))
- {
- MySqlCommand cmd = new MySqlCommand();
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
- {
- DataSet ds = new DataSet();
- try
- {
- da.Fill(ds, "ds");
- cmd.Parameters.Clear();
- }
- catch (MySql.Data.MySqlClient.MySqlException ex)
- {
- throw new Exception(ex.Message);
- }
- return ds;
- }
- }
- }
- private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, string cmdText, MySqlParameter[] cmdParms)
- {
- if (conn.State != ConnectionState.Open)
- conn.Open();
- cmd.Connection = conn;
- cmd.CommandText = cmdText;
- if (trans != null)
- cmd.Transaction = trans;
- cmd.CommandType = CommandType.Text;
- if (cmdParms != null)
- {
- foreach (MySqlParameter parameter in cmdParms)
- {
- if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
- (parameter.Value == null))
- {
- parameter.Value = DBNull.Value;
- }
- cmd.Parameters.Add(parameter);
- }
- }
- }
- public static DataTable Fill(String sql)
- {
- DataTable dt = null;
- try
- {
- DataSet ds = Query(sql);
- if (ds.Tables.Count > 0)
- {
- dt = ds.Tables[0];
- }
- }
- catch (Exception ex)
- {
- dt = null;
-
- }
- return dt;
- }
- #endregion
- }
- }
|