You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
870 lines
33 KiB
870 lines
33 KiB
9 months ago
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Specialized;
|
||
|
using System.Data;
|
||
|
|
||
|
using System.Configuration;
|
||
|
using System.Data.Common;
|
||
|
using System.Collections.Generic;
|
||
|
using MySql.Data.MySqlClient;
|
||
|
|
||
|
namespace FangYar.DBUtility
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// ���ݷ��ʳ���������
|
||
|
/// Copyright (C) FangYar
|
||
|
/// </summary>
|
||
|
public abstract class DbHelperMySQL
|
||
|
{
|
||
|
//���ݿ������ַ���(web.config������)�����Զ�̬����connectionString֧�ֶ����ݿ�.
|
||
|
public static string connectionString = PubConstant.ConnectionString;
|
||
|
public DbHelperMySQL()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
#region ���÷���
|
||
|
/// <summary>
|
||
|
/// �õ�����ֵ
|
||
|
/// </summary>
|
||
|
/// <param name="FieldName"></param>
|
||
|
/// <param name="TableName"></param>
|
||
|
/// <returns></returns>
|
||
|
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());
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// �Ƿ�����
|
||
|
/// </summary>
|
||
|
/// <param name="strSql"></param>
|
||
|
/// <returns></returns>
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// �Ƿ����ڣ�����MySqlParameter��
|
||
|
/// </summary>
|
||
|
/// <param name="strSql"></param>
|
||
|
/// <param name="cmdParms"></param>
|
||
|
/// <returns></returns>
|
||
|
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����
|
||
|
|
||
|
/// <summary>
|
||
|
/// ִ��SQL���䣬����Ӱ���ļ�¼��
|
||
|
/// </summary>
|
||
|
/// <param name="SQLString">SQL����</param>
|
||
|
/// <returns>Ӱ���ļ�¼��</returns>
|
||
|
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.MyException 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.MyException e)
|
||
|
{
|
||
|
connection.Close();
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// ִ��Sql��Oracle�λ�������
|
||
|
/// </summary>
|
||
|
/// <param name="list">SQL�������б�</param>
|
||
|
/// <param name="oracleCmdSqlList">Oracle�������б�</param>
|
||
|
/// <returns>ִ�н��� 0-����SQL��������ʧ�� -1 ����Oracle��������ʧ�� 1-��������ִ�гɹ�</returns>
|
||
|
public static int ExecuteSqlTran(List<CommandInfo> list, List<CommandInfo> oracleCmdSqlList)
|
||
|
{
|
||
|
using (MySqlConnection conn = new MySqlConnection(connectionString))
|
||
|
{
|
||
|
conn.Open();
|
||
|
MySqlCommand cmd = new MySqlCommand();
|
||
|
cmd.Connection = conn;
|
||
|
MySqlTransaction tx = conn.BeginTransaction();
|
||
|
cmd.Transaction = tx;
|
||
|
try
|
||
|
{
|
||
|
foreach (CommandInfo myDE in list)
|
||
|
{
|
||
|
string cmdText = myDE.CommandText;
|
||
|
MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Parameters;
|
||
|
PrepareCommand(cmd, conn, tx, cmdText, cmdParms);
|
||
|
if (myDE.EffentNextType == EffentNextType.SolicitationEvent)
|
||
|
{
|
||
|
if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
|
||
|
{
|
||
|
tx.Rollback();
|
||
|
throw new Exception("Υ��Ҫ��"+myDE.CommandText+"��������select count(..�ĸ�ʽ");
|
||
|
//return 0;
|
||
|
}
|
||
|
|
||
|
object obj = cmd.ExecuteScalar();
|
||
|
bool isHave = false;
|
||
|
if (obj == null && obj == DBNull.Value)
|
||
|
{
|
||
|
isHave = false;
|
||
|
}
|
||
|
isHave = Convert.ToInt32(obj) > 0;
|
||
|
if (isHave)
|
||
|
{
|
||
|
//�����¼�
|
||
|
myDE.OnSolicitationEvent();
|
||
|
}
|
||
|
}
|
||
|
if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine)
|
||
|
{
|
||
|
if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
|
||
|
{
|
||
|
tx.Rollback();
|
||
|
throw new Exception("SQL:Υ��Ҫ��" + myDE.CommandText + "��������select count(..�ĸ�ʽ");
|
||
|
//return 0;
|
||
|
}
|
||
|
|
||
|
object obj = cmd.ExecuteScalar();
|
||
|
bool isHave = false;
|
||
|
if (obj == null && obj == DBNull.Value)
|
||
|
{
|
||
|
isHave = false;
|
||
|
}
|
||
|
isHave = Convert.ToInt32(obj) > 0;
|
||
|
|
||
|
if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave)
|
||
|
{
|
||
|
tx.Rollback();
|
||
|
throw new Exception("SQL:Υ��Ҫ��" + myDE.CommandText + "����ֵ��������0");
|
||
|
//return 0;
|
||
|
}
|
||
|
if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave)
|
||
|
{
|
||
|
tx.Rollback();
|
||
|
throw new Exception("SQL:Υ��Ҫ��" + myDE.CommandText + "����ֵ��������0");
|
||
|
//return 0;
|
||
|
}
|
||
|
continue;
|
||
|
}
|
||
|
int val = cmd.ExecuteNonQuery();
|
||
|
if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0)
|
||
|
{
|
||
|
tx.Rollback();
|
||
|
throw new Exception("SQL:Υ��Ҫ��" + myDE.CommandText + "������Ӱ����");
|
||
|
//return 0;
|
||
|
}
|
||
|
cmd.Parameters.Clear();
|
||
|
}
|
||
|
string oraConnectionString = PubConstant.GetConnectionString("ConnectionStringPPC");
|
||
|
bool res = OracleHelper.ExecuteSqlTran(oraConnectionString, oracleCmdSqlList);
|
||
|
if (!res)
|
||
|
{
|
||
|
tx.Rollback();
|
||
|
throw new Exception("ִ��ʧ��");
|
||
|
// return -1;
|
||
|
}
|
||
|
tx.Commit();
|
||
|
return 1;
|
||
|
}
|
||
|
catch (MySql.Data.MySqlClient.MyException e)
|
||
|
{
|
||
|
tx.Rollback();
|
||
|
throw e;
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
tx.Rollback();
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// ִ�ж���SQL���䣬ʵ�����ݿ�������
|
||
|
/// </summary>
|
||
|
/// <param name="SQLStringList">����SQL����</param>
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// ִ�д�һ���洢���̲����ĵ�SQL���䡣
|
||
|
/// </summary>
|
||
|
/// <param name="SQLString">SQL����</param>
|
||
|
/// <param name="content">��������,����һ���ֶ��Ǹ�ʽ���ӵ����£����������ţ�����ͨ��������ʽ����</param>
|
||
|
/// <returns>Ӱ���ļ�¼��</returns>
|
||
|
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.MyException e)
|
||
|
{
|
||
|
throw e;
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
cmd.Dispose();
|
||
|
connection.Close();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// ִ�д�һ���洢���̲����ĵ�SQL���䡣
|
||
|
/// </summary>
|
||
|
/// <param name="SQLString">SQL����</param>
|
||
|
/// <param name="content">��������,����һ���ֶ��Ǹ�ʽ���ӵ����£����������ţ�����ͨ��������ʽ����</param>
|
||
|
/// <returns>Ӱ���ļ�¼��</returns>
|
||
|
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.MyException e)
|
||
|
{
|
||
|
throw e;
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
cmd.Dispose();
|
||
|
connection.Close();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// �����ݿ�������ͼ����ʽ���ֶ�(�������������Ƶ���һ��ʵ��)
|
||
|
/// </summary>
|
||
|
/// <param name="strSQL">SQL����</param>
|
||
|
/// <param name="fs">ͼ���ֽ�,���ݿ����ֶ�����Ϊimage������</param>
|
||
|
/// <returns>Ӱ���ļ�¼��</returns>
|
||
|
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.MyException e)
|
||
|
{
|
||
|
throw e;
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
cmd.Dispose();
|
||
|
connection.Close();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// ִ��һ��������ѯ�������䣬���ز�ѯ������object����
|
||
|
/// </summary>
|
||
|
/// <param name="SQLString">������ѯ��������</param>
|
||
|
/// <returns>��ѯ������object��</returns>
|
||
|
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.MyException 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.MyException e)
|
||
|
{
|
||
|
connection.Close();
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// ִ�в�ѯ���䣬����MySqlDataReader ( ע�⣺���ø÷�������һ��Ҫ��MySqlDataReader����Close )
|
||
|
/// </summary>
|
||
|
/// <param name="strSQL">��ѯ����</param>
|
||
|
/// <returns>MySqlDataReader</returns>
|
||
|
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.MyException e)
|
||
|
{
|
||
|
throw e;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// ִ�в�ѯ���䣬����DataSet
|
||
|
/// </summary>
|
||
|
/// <param name="SQLString">��ѯ����</param>
|
||
|
/// <returns>DataSet</returns>
|
||
|
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.MyException 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.MyException ex)
|
||
|
{
|
||
|
throw new Exception(ex.Message);
|
||
|
}
|
||
|
return ds;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region ִ�д�������SQL����
|
||
|
|
||
|
/// <summary>
|
||
|
/// ִ��SQL���䣬����Ӱ���ļ�¼��
|
||
|
/// </summary>
|
||
|
/// <param name="SQLString">SQL����</param>
|
||
|
/// <returns>Ӱ���ļ�¼��</returns>
|
||
|
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.MyException e)
|
||
|
{
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// ִ�ж���SQL���䣬ʵ�����ݿ�������
|
||
|
/// </summary>
|
||
|
/// <param name="SQLStringList">SQL�����Ĺ�ϣ����keyΪsql���䣬value�Ǹ�������MySqlParameter[]��</param>
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// ִ�ж���SQL���䣬ʵ�����ݿ�������
|
||
|
/// </summary>
|
||
|
/// <param name="SQLStringList">SQL�����Ĺ�ϣ����keyΪsql���䣬value�Ǹ�������MySqlParameter[]��</param>
|
||
|
public static int ExecuteSqlTran(System.Collections.Generic.List<CommandInfo> cmdList)
|
||
|
{
|
||
|
using (MySqlConnection conn = new MySqlConnection(connectionString))
|
||
|
{
|
||
|
conn.Open();
|
||
|
using (MySqlTransaction trans = conn.BeginTransaction())
|
||
|
{
|
||
|
MySqlCommand cmd = new MySqlCommand();
|
||
|
try
|
||
|
{ int count = 0;
|
||
|
//ѭ��
|
||
|
foreach (CommandInfo myDE in cmdList)
|
||
|
{
|
||
|
string cmdText = myDE.CommandText;
|
||
|
MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Parameters;
|
||
|
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
|
||
|
|
||
|
if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine)
|
||
|
{
|
||
|
if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
|
||
|
{
|
||
|
trans.Rollback();
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
object obj = cmd.ExecuteScalar();
|
||
|
bool isHave = false;
|
||
|
if (obj == null && obj == DBNull.Value)
|
||
|
{
|
||
|
isHave = false;
|
||
|
}
|
||
|
isHave = Convert.ToInt32(obj) > 0;
|
||
|
|
||
|
if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave)
|
||
|
{
|
||
|
trans.Rollback();
|
||
|
return 0;
|
||
|
}
|
||
|
if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave)
|
||
|
{
|
||
|
trans.Rollback();
|
||
|
return 0;
|
||
|
}
|
||
|
continue;
|
||
|
}
|
||
|
int val = cmd.ExecuteNonQuery();
|
||
|
count += val;
|
||
|
if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0)
|
||
|
{
|
||
|
trans.Rollback();
|
||
|
return 0;
|
||
|
}
|
||
|
cmd.Parameters.Clear();
|
||
|
}
|
||
|
trans.Commit();
|
||
|
return count;
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
trans.Rollback();
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// ִ�ж���SQL���䣬ʵ�����ݿ�������
|
||
|
/// </summary>
|
||
|
/// <param name="SQLStringList">SQL�����Ĺ�ϣ����keyΪsql���䣬value�Ǹ�������MySqlParameter[]��</param>
|
||
|
public static void ExecuteSqlTranWithIndentity(System.Collections.Generic.List<CommandInfo> SQLStringList)
|
||
|
{
|
||
|
using (MySqlConnection conn = new MySqlConnection(connectionString))
|
||
|
{
|
||
|
conn.Open();
|
||
|
using (MySqlTransaction trans = conn.BeginTransaction())
|
||
|
{
|
||
|
MySqlCommand cmd = new MySqlCommand();
|
||
|
try
|
||
|
{
|
||
|
int indentity = 0;
|
||
|
//ѭ��
|
||
|
foreach (CommandInfo myDE in SQLStringList)
|
||
|
{
|
||
|
string cmdText = myDE.CommandText;
|
||
|
MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Parameters;
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// ִ�ж���SQL���䣬ʵ�����ݿ�������
|
||
|
/// </summary>
|
||
|
/// <param name="SQLStringList">SQL�����Ĺ�ϣ����keyΪsql���䣬value�Ǹ�������MySqlParameter[]��</param>
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// ִ��һ��������ѯ�������䣬���ز�ѯ������object����
|
||
|
/// </summary>
|
||
|
/// <param name="SQLString">������ѯ��������</param>
|
||
|
/// <returns>��ѯ������object��</returns>
|
||
|
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.MyException e)
|
||
|
{
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// ִ�в�ѯ���䣬����MySqlDataReader ( ע�⣺���ø÷�������һ��Ҫ��MySqlDataReader����Close )
|
||
|
/// </summary>
|
||
|
/// <param name="strSQL">��ѯ����</param>
|
||
|
/// <returns>MySqlDataReader</returns>
|
||
|
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.MyException e)
|
||
|
{
|
||
|
throw e;
|
||
|
}
|
||
|
// finally
|
||
|
// {
|
||
|
// cmd.Dispose();
|
||
|
// connection.Close();
|
||
|
// }
|
||
|
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// ִ�в�ѯ���䣬����DataSet
|
||
|
/// </summary>
|
||
|
/// <param name="SQLString">��ѯ����</param>
|
||
|
/// <returns>DataSet</returns>
|
||
|
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.MyException 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;//cmdType;
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|