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.
1020 lines
39 KiB
1020 lines
39 KiB
9 months ago
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Specialized;
|
||
|
using System.Data;
|
||
|
using System.Data.SqlClient;
|
||
|
using System.Configuration;
|
||
|
using System.Data.Common;
|
||
|
using System.Collections.Generic;
|
||
|
namespace FangYar.DBUtility
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// ���ݷ����࣬�����ڷ��ʲ�ͬ���ݿ�
|
||
|
/// Copyright (C) FangYar
|
||
|
/// </summary>
|
||
|
public class DbHelperSQLP
|
||
|
{
|
||
|
//���ݿ������ַ���(web.config������)�����Զ�̬����connectionString֧�ֶ����ݿ�.
|
||
|
public string connectionString = PubConstant.ConnectionString;
|
||
|
public DbHelperSQLP()
|
||
|
{
|
||
|
}
|
||
|
public DbHelperSQLP(string ConnectionString)
|
||
|
{
|
||
|
connectionString = ConnectionString;
|
||
|
}
|
||
|
|
||
|
#region ���÷���
|
||
|
/// <summary>
|
||
|
/// �ж��Ƿ�����ij����ij���ֶ�
|
||
|
/// </summary>
|
||
|
/// <param name="tableName">������</param>
|
||
|
/// <param name="columnName">������</param>
|
||
|
/// <returns>�Ƿ�����</returns>
|
||
|
public bool ColumnExists(string tableName, string columnName)
|
||
|
{
|
||
|
string sql = "select count(1) from syscolumns where [id]=object_id('" + tableName + "') and [name]='" + columnName + "'";
|
||
|
object res = GetSingle(sql);
|
||
|
if (res == null)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
return Convert.ToInt32(res) > 0;
|
||
|
}
|
||
|
public 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 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>
|
||
|
/// ���Ƿ�����
|
||
|
/// </summary>
|
||
|
/// <param name="TableName"></param>
|
||
|
/// <returns></returns>
|
||
|
public bool TabExists(string TableName)
|
||
|
{
|
||
|
string strsql = "select count(*) from sysobjects where id = object_id(N'[" + TableName + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1";
|
||
|
//string strsql = "SELECT count(*) FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[" + TableName + "]') AND type in (N'U')";
|
||
|
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 bool Exists(string strSql, params SqlParameter[] 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 int ExecuteSql(string SQLString)
|
||
|
{
|
||
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
||
|
{
|
||
|
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
connection.Open();
|
||
|
int rows = cmd.ExecuteNonQuery();
|
||
|
return rows;
|
||
|
}
|
||
|
catch (System.Data.SqlClient.Exception e)
|
||
|
{
|
||
|
connection.Close();
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public int ExecuteSqlByTime(string SQLString, int Times)
|
||
|
{
|
||
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
||
|
{
|
||
|
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
connection.Open();
|
||
|
cmd.CommandTimeout = Times;
|
||
|
int rows = cmd.ExecuteNonQuery();
|
||
|
return rows;
|
||
|
}
|
||
|
catch (System.Data.SqlClient.Exception 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 int ExecuteSqlTran(List<CommandInfo> list, List<CommandInfo> oracleCmdSqlList)
|
||
|
{
|
||
|
using (SqlConnection conn = new SqlConnection(connectionString))
|
||
|
{
|
||
|
conn.Open();
|
||
|
SqlCommand cmd = new SqlCommand();
|
||
|
cmd.Connection = conn;
|
||
|
SqlTransaction tx = conn.BeginTransaction();
|
||
|
cmd.Transaction = tx;
|
||
|
try
|
||
|
{
|
||
|
foreach (CommandInfo myDE in list)
|
||
|
{
|
||
|
string cmdText = myDE.CommandText;
|
||
|
SqlParameter[] cmdParms = (SqlParameter[])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("Oracleִ��ʧ��");
|
||
|
// return -1;
|
||
|
}
|
||
|
tx.Commit();
|
||
|
return 1;
|
||
|
}
|
||
|
catch (System.Data.SqlClient.Exception e)
|
||
|
{
|
||
|
tx.Rollback();
|
||
|
throw e;
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
tx.Rollback();
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// ִ�ж���SQL���䣬ʵ�����ݿ�������
|
||
|
/// </summary>
|
||
|
/// <param name="SQLStringList">����SQL����</param>
|
||
|
public int ExecuteSqlTran(List<String> SQLStringList)
|
||
|
{
|
||
|
using (SqlConnection conn = new SqlConnection(connectionString))
|
||
|
{
|
||
|
conn.Open();
|
||
|
SqlCommand cmd = new SqlCommand();
|
||
|
cmd.Connection = conn;
|
||
|
SqlTransaction 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 int ExecuteSql(string SQLString, string content)
|
||
|
{
|
||
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
||
|
{
|
||
|
SqlCommand cmd = new SqlCommand(SQLString, connection);
|
||
|
System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText);
|
||
|
myParameter.Value = content;
|
||
|
cmd.Parameters.Add(myParameter);
|
||
|
try
|
||
|
{
|
||
|
connection.Open();
|
||
|
int rows = cmd.ExecuteNonQuery();
|
||
|
return rows;
|
||
|
}
|
||
|
catch (System.Data.SqlClient.Exception e)
|
||
|
{
|
||
|
throw e;
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
cmd.Dispose();
|
||
|
connection.Close();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// ִ�д�һ���洢���̲����ĵ�SQL���䡣
|
||
|
/// </summary>
|
||
|
/// <param name="SQLString">SQL����</param>
|
||
|
/// <param name="content">��������,����һ���ֶ��Ǹ�ʽ���ӵ����£����������ţ�����ͨ��������ʽ����</param>
|
||
|
/// <returns>Ӱ���ļ�¼��</returns>
|
||
|
public object ExecuteSqlGet(string SQLString, string content)
|
||
|
{
|
||
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
||
|
{
|
||
|
SqlCommand cmd = new SqlCommand(SQLString, connection);
|
||
|
System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@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 (System.Data.SqlClient.Exception e)
|
||
|
{
|
||
|
throw e;
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
cmd.Dispose();
|
||
|
connection.Close();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// �����ݿ�������ͼ����ʽ���ֶ�(�������������Ƶ���һ��ʵ��)
|
||
|
/// </summary>
|
||
|
/// <param name="strSQL">SQL����</param>
|
||
|
/// <param name="fs">ͼ���ֽ�,���ݿ����ֶ�����Ϊimage������</param>
|
||
|
/// <returns>Ӱ���ļ�¼��</returns>
|
||
|
public int ExecuteSqlInsertImg(string strSQL, byte[] fs)
|
||
|
{
|
||
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
||
|
{
|
||
|
SqlCommand cmd = new SqlCommand(strSQL, connection);
|
||
|
System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@fs", SqlDbType.Image);
|
||
|
myParameter.Value = fs;
|
||
|
cmd.Parameters.Add(myParameter);
|
||
|
try
|
||
|
{
|
||
|
connection.Open();
|
||
|
int rows = cmd.ExecuteNonQuery();
|
||
|
return rows;
|
||
|
}
|
||
|
catch (System.Data.SqlClient.Exception e)
|
||
|
{
|
||
|
throw e;
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
cmd.Dispose();
|
||
|
connection.Close();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// ִ��һ��������ѯ�������䣬���ز�ѯ������object����
|
||
|
/// </summary>
|
||
|
/// <param name="SQLString">������ѯ��������</param>
|
||
|
/// <returns>��ѯ������object��</returns>
|
||
|
public object GetSingle(string SQLString)
|
||
|
{
|
||
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
||
|
{
|
||
|
using (SqlCommand cmd = new SqlCommand(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 (System.Data.SqlClient.Exception e)
|
||
|
{
|
||
|
connection.Close();
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
public object GetSingle(string SQLString, int Times)
|
||
|
{
|
||
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
||
|
{
|
||
|
using (SqlCommand cmd = new SqlCommand(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 (System.Data.SqlClient.Exception e)
|
||
|
{
|
||
|
connection.Close();
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// ִ�в�ѯ���䣬����SqlDataReader ( ע�⣺���ø÷�������һ��Ҫ��SqlDataReader����Close )
|
||
|
/// </summary>
|
||
|
/// <param name="strSQL">��ѯ����</param>
|
||
|
/// <returns>SqlDataReader</returns>
|
||
|
public SqlDataReader ExecuteReader(string strSQL)
|
||
|
{
|
||
|
SqlConnection connection = new SqlConnection(connectionString);
|
||
|
SqlCommand cmd = new SqlCommand(strSQL, connection);
|
||
|
try
|
||
|
{
|
||
|
connection.Open();
|
||
|
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
||
|
return myReader;
|
||
|
}
|
||
|
catch (System.Data.SqlClient.Exception e)
|
||
|
{
|
||
|
throw e;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// ִ�в�ѯ���䣬����DataSet
|
||
|
/// </summary>
|
||
|
/// <param name="SQLString">��ѯ����</param>
|
||
|
/// <returns>DataSet</returns>
|
||
|
public DataSet Query(string SQLString)
|
||
|
{
|
||
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
||
|
{
|
||
|
DataSet ds = new DataSet();
|
||
|
try
|
||
|
{
|
||
|
connection.Open();
|
||
|
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
|
||
|
command.Fill(ds, "ds");
|
||
|
}
|
||
|
catch (System.Data.SqlClient.Exception ex)
|
||
|
{
|
||
|
throw new Exception(ex.Message);
|
||
|
}
|
||
|
return ds;
|
||
|
}
|
||
|
}
|
||
|
public DataSet Query(string SQLString, int Times)
|
||
|
{
|
||
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
||
|
{
|
||
|
DataSet ds = new DataSet();
|
||
|
try
|
||
|
{
|
||
|
connection.Open();
|
||
|
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
|
||
|
command.SelectCommand.CommandTimeout = Times;
|
||
|
command.Fill(ds, "ds");
|
||
|
}
|
||
|
catch (System.Data.SqlClient.Exception ex)
|
||
|
{
|
||
|
throw new Exception(ex.Message);
|
||
|
}
|
||
|
return ds;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region ִ�д�������SQL����
|
||
|
|
||
|
/// <summary>
|
||
|
/// ִ��SQL���䣬����Ӱ���ļ�¼��
|
||
|
/// </summary>
|
||
|
/// <param name="SQLString">SQL����</param>
|
||
|
/// <returns>Ӱ���ļ�¼��</returns>
|
||
|
public int ExecuteSql(string SQLString, params SqlParameter[] cmdParms)
|
||
|
{
|
||
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
||
|
{
|
||
|
using (SqlCommand cmd = new SqlCommand())
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||
|
int rows = cmd.ExecuteNonQuery();
|
||
|
cmd.Parameters.Clear();
|
||
|
return rows;
|
||
|
}
|
||
|
catch (System.Data.SqlClient.Exception e)
|
||
|
{
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// ִ�ж���SQL���䣬ʵ�����ݿ�������
|
||
|
/// </summary>
|
||
|
/// <param name="SQLStringList">SQL�����Ĺ�ϣ����keyΪsql���䣬value�Ǹ�������SqlParameter[]��</param>
|
||
|
public void ExecuteSqlTran(Hashtable SQLStringList)
|
||
|
{
|
||
|
using (SqlConnection conn = new SqlConnection(connectionString))
|
||
|
{
|
||
|
conn.Open();
|
||
|
using (SqlTransaction trans = conn.BeginTransaction())
|
||
|
{
|
||
|
SqlCommand cmd = new SqlCommand();
|
||
|
try
|
||
|
{
|
||
|
//ѭ��
|
||
|
foreach (DictionaryEntry myDE in SQLStringList)
|
||
|
{
|
||
|
string cmdText = myDE.Key.ToString();
|
||
|
SqlParameter[] cmdParms = (SqlParameter[])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�Ǹ�������SqlParameter[]��</param>
|
||
|
public int ExecuteSqlTran(System.Collections.Generic.List<CommandInfo> cmdList)
|
||
|
{
|
||
|
using (SqlConnection conn = new SqlConnection(connectionString))
|
||
|
{
|
||
|
conn.Open();
|
||
|
using (SqlTransaction trans = conn.BeginTransaction())
|
||
|
{
|
||
|
SqlCommand cmd = new SqlCommand();
|
||
|
try
|
||
|
{ int count = 0;
|
||
|
//ѭ��
|
||
|
foreach (CommandInfo myDE in cmdList)
|
||
|
{
|
||
|
string cmdText = myDE.CommandText;
|
||
|
SqlParameter[] cmdParms = (SqlParameter[])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�Ǹ�������SqlParameter[]��</param>
|
||
|
public void ExecuteSqlTranWithIndentity(System.Collections.Generic.List<CommandInfo> SQLStringList)
|
||
|
{
|
||
|
using (SqlConnection conn = new SqlConnection(connectionString))
|
||
|
{
|
||
|
conn.Open();
|
||
|
using (SqlTransaction trans = conn.BeginTransaction())
|
||
|
{
|
||
|
SqlCommand cmd = new SqlCommand();
|
||
|
try
|
||
|
{
|
||
|
int indentity = 0;
|
||
|
//ѭ��
|
||
|
foreach (CommandInfo myDE in SQLStringList)
|
||
|
{
|
||
|
string cmdText = myDE.CommandText;
|
||
|
SqlParameter[] cmdParms = (SqlParameter[])myDE.Parameters;
|
||
|
foreach (SqlParameter q in cmdParms)
|
||
|
{
|
||
|
if (q.Direction == ParameterDirection.InputOutput)
|
||
|
{
|
||
|
q.Value = indentity;
|
||
|
}
|
||
|
}
|
||
|
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
|
||
|
int val = cmd.ExecuteNonQuery();
|
||
|
foreach (SqlParameter 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�Ǹ�������SqlParameter[]��</param>
|
||
|
public void ExecuteSqlTranWithIndentity(Hashtable SQLStringList)
|
||
|
{
|
||
|
using (SqlConnection conn = new SqlConnection(connectionString))
|
||
|
{
|
||
|
conn.Open();
|
||
|
using (SqlTransaction trans = conn.BeginTransaction())
|
||
|
{
|
||
|
SqlCommand cmd = new SqlCommand();
|
||
|
try
|
||
|
{
|
||
|
int indentity = 0;
|
||
|
//ѭ��
|
||
|
foreach (DictionaryEntry myDE in SQLStringList)
|
||
|
{
|
||
|
string cmdText = myDE.Key.ToString();
|
||
|
SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;
|
||
|
foreach (SqlParameter q in cmdParms)
|
||
|
{
|
||
|
if (q.Direction == ParameterDirection.InputOutput)
|
||
|
{
|
||
|
q.Value = indentity;
|
||
|
}
|
||
|
}
|
||
|
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
|
||
|
int val = cmd.ExecuteNonQuery();
|
||
|
foreach (SqlParameter 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 object GetSingle(string SQLString, params SqlParameter[] cmdParms)
|
||
|
{
|
||
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
||
|
{
|
||
|
using (SqlCommand cmd = new SqlCommand())
|
||
|
{
|
||
|
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 (System.Data.SqlClient.Exception e)
|
||
|
{
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// ִ�в�ѯ���䣬����SqlDataReader ( ע�⣺���ø÷�������һ��Ҫ��SqlDataReader����Close )
|
||
|
/// </summary>
|
||
|
/// <param name="strSQL">��ѯ����</param>
|
||
|
/// <returns>SqlDataReader</returns>
|
||
|
public SqlDataReader ExecuteReader(string SQLString, params SqlParameter[] cmdParms)
|
||
|
{
|
||
|
SqlConnection connection = new SqlConnection(connectionString);
|
||
|
SqlCommand cmd = new SqlCommand();
|
||
|
try
|
||
|
{
|
||
|
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||
|
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
||
|
cmd.Parameters.Clear();
|
||
|
return myReader;
|
||
|
}
|
||
|
catch (System.Data.SqlClient.Exception e)
|
||
|
{
|
||
|
throw e;
|
||
|
}
|
||
|
// finally
|
||
|
// {
|
||
|
// cmd.Dispose();
|
||
|
// connection.Close();
|
||
|
// }
|
||
|
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// ִ�в�ѯ���䣬����DataSet
|
||
|
/// </summary>
|
||
|
/// <param name="SQLString">��ѯ����</param>
|
||
|
/// <returns>DataSet</returns>
|
||
|
public DataSet Query(string SQLString, params SqlParameter[] cmdParms)
|
||
|
{
|
||
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
||
|
{
|
||
|
SqlCommand cmd = new SqlCommand();
|
||
|
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||
|
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
|
||
|
{
|
||
|
DataSet ds = new DataSet();
|
||
|
try
|
||
|
{
|
||
|
da.Fill(ds, "ds");
|
||
|
cmd.Parameters.Clear();
|
||
|
}
|
||
|
catch (System.Data.SqlClient.Exception ex)
|
||
|
{
|
||
|
throw new Exception(ex.Message);
|
||
|
}
|
||
|
return ds;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] 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 (SqlParameter parameter in cmdParms)
|
||
|
{
|
||
|
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
|
||
|
(parameter.Value == null))
|
||
|
{
|
||
|
parameter.Value = DBNull.Value;
|
||
|
}
|
||
|
cmd.Parameters.Add(parameter);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region �洢���̲���
|
||
|
|
||
|
/// <summary>
|
||
|
/// ִ�д洢���̣�����SqlDataReader ( ע�⣺���ø÷�������һ��Ҫ��SqlDataReader����Close )
|
||
|
/// </summary>
|
||
|
/// <param name="storedProcName">�洢������</param>
|
||
|
/// <param name="parameters">�洢���̲���</param>
|
||
|
/// <returns>SqlDataReader</returns>
|
||
|
public SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
|
||
|
{
|
||
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
||
|
{
|
||
|
SqlDataReader returnReader;
|
||
|
connection.Open();
|
||
|
SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
|
||
|
command.CommandType = CommandType.StoredProcedure;
|
||
|
returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
|
||
|
return returnReader;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// ִ�д洢����
|
||
|
/// </summary>
|
||
|
/// <param name="storedProcName">�洢������</param>
|
||
|
/// <param name="parameters">�洢���̲���</param>
|
||
|
/// <param name="tableName">DataSet�����еı���</param>
|
||
|
/// <returns>DataSet</returns>
|
||
|
public DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
|
||
|
{
|
||
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
||
|
{
|
||
|
DataSet dataSet = new DataSet();
|
||
|
connection.Open();
|
||
|
SqlDataAdapter sqlDA = new SqlDataAdapter();
|
||
|
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
|
||
|
sqlDA.Fill(dataSet, tableName);
|
||
|
connection.Close();
|
||
|
return dataSet;
|
||
|
}
|
||
|
}
|
||
|
public DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName, int Times)
|
||
|
{
|
||
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
||
|
{
|
||
|
DataSet dataSet = new DataSet();
|
||
|
connection.Open();
|
||
|
SqlDataAdapter sqlDA = new SqlDataAdapter();
|
||
|
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
|
||
|
sqlDA.SelectCommand.CommandTimeout = Times;
|
||
|
sqlDA.Fill(dataSet, tableName);
|
||
|
connection.Close();
|
||
|
return dataSet;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// ���� SqlCommand ����(��������һ����������������һ������ֵ)
|
||
|
/// </summary>
|
||
|
/// <param name="connection">���ݿ�����</param>
|
||
|
/// <param name="storedProcName">�洢������</param>
|
||
|
/// <param name="parameters">�洢���̲���</param>
|
||
|
/// <returns>SqlCommand</returns>
|
||
|
private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
|
||
|
{
|
||
|
SqlCommand command = new SqlCommand(storedProcName, connection);
|
||
|
command.CommandType = CommandType.StoredProcedure;
|
||
|
foreach (SqlParameter parameter in parameters)
|
||
|
{
|
||
|
if (parameter != null)
|
||
|
{
|
||
|
// ����δ����ֵ����������,����������DBNull.Value.
|
||
|
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
|
||
|
(parameter.Value == null))
|
||
|
{
|
||
|
parameter.Value = DBNull.Value;
|
||
|
}
|
||
|
command.Parameters.Add(parameter);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return command;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// ִ�д洢���̣�����Ӱ��������
|
||
|
/// </summary>
|
||
|
/// <param name="storedProcName">�洢������</param>
|
||
|
/// <param name="parameters">�洢���̲���</param>
|
||
|
/// <param name="rowsAffected">Ӱ��������</param>
|
||
|
/// <returns></returns>
|
||
|
public int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
|
||
|
{
|
||
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
||
|
{
|
||
|
int result;
|
||
|
connection.Open();
|
||
|
SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
|
||
|
rowsAffected = command.ExecuteNonQuery();
|
||
|
result = (int)command.Parameters["ReturnValue"].Value;
|
||
|
//Connection.Close();
|
||
|
return result;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// ���� SqlCommand ����ʵ��(��������һ������ֵ)
|
||
|
/// </summary>
|
||
|
/// <param name="storedProcName">�洢������</param>
|
||
|
/// <param name="parameters">�洢���̲���</param>
|
||
|
/// <returns>SqlCommand ����ʵ��</returns>
|
||
|
private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
|
||
|
{
|
||
|
SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
|
||
|
command.Parameters.Add(new SqlParameter("ReturnValue",
|
||
|
SqlDbType.Int, 4, ParameterDirection.ReturnValue,
|
||
|
false, 0, 0, string.Empty, DataRowVersion.Default, null));
|
||
|
return command;
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|