当前位置:网站首页>Precipitated database operation class - version C (SQL Server)
Precipitated database operation class - version C (SQL Server)
2022-07-06 17:26:00 【Heart blue 168】
#region Namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
#endregion
namespace XL.DB.DbOperaClass
{
#region SQL Sentence structure
/// <summary>
/// Sql Sentence structure
/// </summary>
public class SqlStruct
{
#region Variable
/// <summary>
/// sql sentence
/// </summary>
public string sql = string.Empty; //Sql sentence
/// <summary>
/// sql Statement parameter list ( Optional )
/// </summary>
public object[] para = null; // Parameter data
#endregion
}
#endregion
#region Data parameter structure
/// <summary>
/// Data parameter structure
/// </summary>
[Serializable]
public class DbParameterStruct
{
#region Variable
/// <summary>
/// Parameter name
/// </summary>
public string ParaName = string.Empty; // Parameter name
/// <summary>
/// Parameter values
/// </summary>
public object ParaValue = null; // Parameter values
#endregion
}
#endregion
/// <summary>
/// Database operation class
/// </summary>
public class DbConnClass
{
#region Variable
private string connectionString = string.Empty;
#endregion
#region [ attribute ] Database connection string
/// <summary>
/// Database connection string
/// </summary>
public string ConnectionString
{
get
{
return this.connectionString;
}
set
{
this.connectionString = value;
}
}
#endregion
#region [ Method ] Open database connection
/// <summary>
/// Open database connection
/// </summary>
public virtual object OpenDbConnection()
{
return null;
}
#endregion
#region [ Method ] Close database connection
/// <summary>
/// Close database connection
/// </summary>
public virtual void CloseDbConnection(object dbConnectObj)
{
}
#endregion
#region [ Method ] Execution returns only those that affect the number of rows Sql sentence
/// <summary>
/// Execution returns only those that affect the number of rows Sql sentence
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <returns> Number of rows affected </returns>
public virtual int ExecuteNonQuery(string sql)
{
return -1;
}
#endregion
#region [ Method ] Execution returns only those that affect the number of rows Sql sentence < Parameter form >
/// <summary>
/// Execution returns only those that affect the number of rows Sql sentence
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraArray"> Parameters of the array </param>
/// <returns> Number of rows affected </returns>
public virtual int ExecuteNonQuery(string sql, object[] paraArray)
{
return -1;
}
#endregion
#region [ Method ] Return and Sql The data table corresponding to the statement
/// <summary>
/// Return and Sql The data table corresponding to the statement
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <returns> Result data sheet </returns>
public virtual DataTable ReturnDataTable(string sql)
{
return null;
}
#endregion
#region [ Method ] Return and Sql The data table corresponding to the statement < Parameter form >
/// <summary>
/// Return and Sql The data table corresponding to the statement
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraArray"> Parameters of the array </param>
/// <returns> Result data sheet </returns>
public virtual DataTable ReturnDataTable(string sql, object[] paraArray)
{
return null;
}
#endregion
#region [ Method ] Returns a dataset containing a single data table
/// <summary>
/// Returns a dataset containing a single data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <returns> Result data set </returns>
public virtual DataSet ReturnDataSet(string sql)
{
return null;
}
#endregion
#region [ Method ] Returns a dataset containing a single data table < Parameter form >
/// <summary>
/// Returns a dataset containing a single data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraArray"> Parameters of the array </param>
/// <returns> Result data set </returns>
public virtual DataSet ReturnDataSet(string sql, object[] paraArray)
{
return null;
}
#endregion
#region [ Method ] Return and Sql Statement the value of the first row and first column of the corresponding data table
/// <summary>
/// Return and Sql Statement the value of the first row and first column of the corresponding data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <returns> The value of the first row and first column of the result data table </returns>
public virtual string ReturnZRowZColContent(string sql)
{
return null;
}
#endregion
#region [ Method ] Return and Sql Statement the value of the first row and first column of the corresponding data table < Parameter form >
/// <summary>
/// Return and Sql Statement the value of the first row and first column of the corresponding data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraArray"> Parameters of the array </param>
/// <returns> The value of the first row and first column of the result data table </returns>
public virtual string ReturnZRowZColContent(string sql, object[] paraArray)
{
return null;
}
#endregion
#region [ Method ] Return and Sql Statement the value of the first row and first column of the corresponding data table (Int)
/// <summary>
/// Return and Sql Statement the value of the first row and first column of the corresponding data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <returns> The value of the first row and first column of the result data table </returns>
public virtual int ReturnZRowZColContentInt(string sql)
{
return -1;
}
#endregion
#region [ Method ] Return and Sql Statement the value of the first row and first column of the corresponding data table (Int)< Parameter form >
/// <summary>
/// Return and Sql Statement the value of the first row and first column of the corresponding data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraArray"> Parameters of the array </param>
/// <returns> The value of the first row and first column of the result data table </returns>
public virtual int ReturnZRowZColContentInt(string sql, object[] paraArray)
{
return -1;
}
#endregion
#region [ Method ] Batch update database data table
/// <summary>
/// Batch update database data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="TableName"> Data sheet to be updated </param>
public virtual void UpdateTable(string sql, System.Data.DataTable TableName)
{
}
#endregion
#region [ Method ] Batch update database data table < Parameter form >
/// <summary>
/// Batch update database data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraArray"> Parameters of the array </param>
/// <param name="TableName"> Data sheet to be updated </param>
public virtual void UpdateTable(string sql, object[] paraArray, System.Data.DataTable TableName)
{
}
#endregion
#region [ Method ] Command mode batch update database
/// <summary>
/// Command mode batch update database
/// </summary>
/// <param name="insertCommand">InsertCommand command </param>
/// <param name="updateCommand">UpdateCommand command </param>
/// <param name="deleteCommand">DeleteCommand command </param>
/// <param name="TableName"> Data sheet to be updated </param>
/// <returns> Successful execution True: success False: Failure </returns>
public virtual bool UpdateTableWithCommand(object insertCommand, object updateCommand, object deleteCommand, DataTable TableName)
{
return true;
}
#endregion
#region [ Method ] Execute stored procedures < Parameter form >
/// <summary>
/// Execute stored procedures
/// </summary>
/// <param name="StoredProcedureName"> Stored procedure name </param>
/// <param name="paraArray"> Parameters of the array </param>
public virtual void ExecuteProcedure(string StoredProcedureName, object[] paraArray)
{
}
#endregion
#region [ Method ] Execute stored procedures < return DataSet>< Parameter form >
/// <summary>
/// Execute stored procedures
/// </summary>
/// <param name="StoredProcedureName"> Stored procedure name </param>
/// <param name="paraArray"> Parameters of the array </param>
/// <returns> Result data set </returns>
public virtual DataSet ExecuteProcedureDs(string StoredProcedureName, object[] paraArray)
{
return null;
}
#endregion
#region [ Method ] Execute a batch and return only those that affect the number of rows SQL sentence < Business >
/// <summary>
/// Execute a batch and return only those that affect the number of rows SQL sentence ( Business )
/// </summary>
/// <param name="sqlList">Sql Statement list </param>
public virtual void ExecuteSqlWidthTransaction(SqlStruct[] sqlList)
{
}
/// <summary>
/// Execute a batch and return only those that affect the number of rows SQL sentence ( Business )
/// </summary>
/// <param name="sqlList">Sql Statement list </param>
public virtual void ExecuteSqlWidthTransaction(List<SqlStruct> sqlList)
{
}
#endregion
public virtual void ExecuteSqlList(List<SqlStruct> sqlList)
{
}
}
}
#region Namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
#endregion
namespace XL.DB.DbOperaClass
{
/// <summary>
/// SQL Database operation class
/// </summary>
public class SqlDbConnClass : DbConnClass
{
#region [ Method ] Constructors
/// <summary>
/// Constructors
/// </summary>
/// <param name="connectionString"> Database connection string example :Server= database IP;uid= user name ;pwd= password ;DataBase= database </param>
public SqlDbConnClass(string connectionString)
{
this.ConnectionString = connectionString;
}
#endregion
#region [ Method ] Open database connection
/// <summary>
/// Open database connection
/// </summary>
public override object OpenDbConnection()
{
SqlConnection sqlConnection = null;
try
{
sqlConnection = new SqlConnection(this.ConnectionString);
sqlConnection.Open();
}
catch (Exception xe)
{
sqlConnection = null;
throw new Exception(xe.Message);
}
return sqlConnection;
}
#endregion
#region [ Method ] Close database connection
/// <summary>
/// Close database connection
/// </summary>
public override void CloseDbConnection(object dbConnectObj)
{
try
{
SqlConnection dbConn = (SqlConnection)dbConnectObj;
if (dbConn != null)
{
dbConn.Close();
}
}
catch
{
//Error
}
}
#endregion
#region [ Method ] Execution returns only those that affect the number of rows Sql sentence
/// <summary>
/// Execution returns only those that affect the number of rows Sql sentence
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <returns> Number of rows affected </returns>
public override int ExecuteNonQuery(string sql)
{
int RowsNum = 0;
SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
RowsNum = sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
}
catch (Exception xe)
{
throw new Exception(xe.Message);
}
finally
{
this.CloseDbConnection(sqlConnection);
}
return RowsNum;
}
#endregion
#region [ Method ] Execution returns only those that affect the number of rows Sql sentence < Parameter form >
/// <summary>
/// Execution returns only those that affect the number of rows Sql sentence
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraList"> Parameters of the array </param>
/// <returns> Number of rows affected </returns>
public int ExecuteNonQuery(string sql, SqlParameter[] paraList)
{
int RowsNum = 0;
SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
foreach (SqlParameter para in paraList)
{
sqlCommand.Parameters.Add(para);
}
RowsNum = sqlCommand.ExecuteNonQuery();
}
catch (Exception xe)
{
throw new Exception(xe.Message);
}
finally
{
this.CloseDbConnection(sqlConnection);
}
return RowsNum;
}
/// <summary>
/// Execution returns only those that affect the number of rows Sql sentence
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraArray"> Parameters of the array </param>
/// <returns> Number of rows affected </returns>
public override int ExecuteNonQuery(string sql, object[] paraArray)
{
SqlParameter[] paraList = null;
if (paraArray is DbParameterStruct[])
{
paraList = new SqlParameter[paraArray.Length];
for (int iLoop = 0; iLoop < paraArray.Length; iLoop++)
{
DbParameterStruct para = (DbParameterStruct)paraArray[iLoop];
paraList[iLoop] = new SqlParameter("@" + para.ParaName, para.ParaValue);
}
}
else
{
paraList = (SqlParameter[])paraArray;
}
return this.ExecuteNonQuery(sql, paraList);
}
#endregion
#region [ Method ] Return and Sql The data table corresponding to the statement
/// <summary>
/// Return and Sql The data table corresponding to the statement
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <returns> Result data sheet </returns>
public override DataTable ReturnDataTable(string sql)
{
DataTable dataTable = new DataTable();
SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sqlConnection);
sqlDataAdapter.Fill(dataTable);
sqlDataAdapter.Dispose();
}
catch (Exception xe)
{
throw new Exception(xe.Message);
}
finally
{
this.CloseDbConnection(sqlConnection);
}
return dataTable;
}
#endregion
#region [ Method ] Return and Sql The data table corresponding to the statement < Parameter form >
/// <summary>
/// Return and Sql The data table corresponding to the statement
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraList"> Parameters of the array </param>
/// <returns> Result data sheet </returns>
public DataTable ReturnDataTable(string sql, SqlParameter[] paraList)
{
DataTable dataTable = new DataTable();
SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sqlConnection);
foreach (SqlParameter para in paraList)
{
sqlDataAdapter.SelectCommand.Parameters.Add(para);
}
sqlDataAdapter.Fill(dataTable);
sqlDataAdapter.Dispose();
}
catch (Exception xe)
{
throw new Exception(xe.Message);
}
finally
{
this.CloseDbConnection(sqlConnection);
}
return dataTable;
}
/// <summary>
/// Return and Sql The data table corresponding to the statement
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraArray"> Parameters of the array </param>
/// <returns> Result data sheet </returns>
public override DataTable ReturnDataTable(string sql, object[] paraArray)
{
SqlParameter[] paraList = null;
if (paraArray is DbParameterStruct[])
{
paraList = new SqlParameter[paraArray.Length];
for (int iLoop = 0; iLoop < paraArray.Length; iLoop++)
{
DbParameterStruct para = (DbParameterStruct)paraArray[iLoop];
paraList[iLoop] = new SqlParameter("@" + para.ParaName, para.ParaValue);
}
}
else
{
paraList = (SqlParameter[])paraArray;
}
return this.ReturnDataTable(sql, paraList);
}
#endregion
#region [ Method ] Returns a dataset containing a single data table
/// <summary>
/// Returns a dataset containing a single data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <returns> Result data set </returns>
public override DataSet ReturnDataSet(string sql)
{
DataSet dataSet = new DataSet();
SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sqlConnection);
sqlDataAdapter.Fill(dataSet);
sqlDataAdapter.Dispose();
}
catch (Exception xe)
{
throw new Exception(xe.Message);
}
finally
{
this.CloseDbConnection(sqlConnection);
}
return dataSet;
}
#endregion
#region [ Method ] Returns a dataset containing a single data table < Parameter form >
/// <summary>
/// Returns a dataset containing a single data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraList"> Parameters of the array </param>
/// <returns> Result data set </returns>
public DataSet ReturnDataSet(string sql, SqlParameter[] paraList)
{
DataSet dataSet = new DataSet();
SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sqlConnection);
foreach (SqlParameter para in paraList)
{
sqlDataAdapter.SelectCommand.Parameters.Add(para);
}
sqlDataAdapter.Fill(dataSet);
sqlDataAdapter.Dispose();
}
catch (Exception xe)
{
throw new Exception(xe.Message);
}
finally
{
this.CloseDbConnection(sqlConnection);
}
return dataSet;
}
/// <summary>
/// Returns a dataset containing a single data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraArray"> Parameters of the array </param>
/// <returns> Result data set </returns>
public override DataSet ReturnDataSet(string sql, object[] paraArray)
{
SqlParameter[] paraList = null;
if (paraArray is DbParameterStruct[])
{
paraList = new SqlParameter[paraArray.Length];
for (int iLoop = 0; iLoop < paraArray.Length; iLoop++)
{
DbParameterStruct para = (DbParameterStruct)paraArray[iLoop];
paraList[iLoop] = new SqlParameter("@" + para.ParaName, para.ParaValue);
}
}
else
{
paraList = (SqlParameter[])paraArray;
}
return this.ReturnDataSet(sql, paraList);
}
#endregion
#region [ Method ] Return and Sql Statement the value of the first row and first column of the corresponding data table
/// <summary>
/// Return and Sql Statement the value of the first row and first column of the corresponding data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <returns> The value of the first row and first column of the result data table </returns>
public override string ReturnZRowZColContent(string sql)
{
string ContentStr = null;
SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
ContentStr = sqlCommand.ExecuteScalar().ToString();
}
catch (Exception xe)
{
throw new Exception(xe.Message);
}
finally
{
this.CloseDbConnection(sqlConnection);
}
return ContentStr;
}
#endregion
#region [ Method ] Return and Sql Statement the value of the first row and first column of the corresponding data table < Parameter form >
/// <summary>
/// Return and Sql Statement the value of the first row and first column of the corresponding data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraList"> Parameters of the array </param>
/// <returns> The value of the first row and first column of the result data table </returns>
public string ReturnZRowZColContent(string sql, SqlParameter[] paraList)
{
string ContentStr = null;
SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
foreach (SqlParameter para in paraList)
{
sqlCommand.Parameters.Add(para);
}
ContentStr = sqlCommand.ExecuteScalar().ToString();
}
catch (Exception xe)
{
throw new Exception(xe.Message);
}
finally
{
this.CloseDbConnection(sqlConnection);
}
return ContentStr;
}
/// <summary>
/// Return and Sql Statement the value of the first row and first column of the corresponding data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraArray"> Parameters of the array </param>
/// <returns> The value of the first row and first column of the result data table </returns>
public override string ReturnZRowZColContent(string sql, object[] paraArray)
{
SqlParameter[] paraList = null;
if (paraArray is DbParameterStruct[])
{
paraList = new SqlParameter[paraArray.Length];
for (int iLoop = 0; iLoop < paraArray.Length; iLoop++)
{
DbParameterStruct para = (DbParameterStruct)paraArray[iLoop];
paraList[iLoop] = new SqlParameter("@" + para.ParaName, para.ParaValue);
}
}
else
{
paraList = (SqlParameter[])paraArray;
}
return this.ReturnZRowZColContent(sql, paraList);
}
#endregion
#region [ Method ] Return and Sql Statement the value of the first row and first column of the corresponding data table (Int)
/// <summary>
/// Return and Sql Statement the value of the first row and first column of the corresponding data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <returns> The value of the first row and first column of the result data table </returns>
public override int ReturnZRowZColContentInt(string sql)
{
int ContentInt = 0;
SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
ContentInt = int.Parse(sqlCommand.ExecuteScalar().ToString());
}
catch (Exception xe)
{
throw new Exception(xe.Message);
}
finally
{
this.CloseDbConnection(sqlConnection);
}
return ContentInt;
}
#endregion
#region [ Method ] Return and Sql Statement the value of the first row and first column of the corresponding data table (Int)< Parameter form >
/// <summary>
/// Return and Sql Statement the value of the first row and first column of the corresponding data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraList"> Parameters of the array </param>
/// <returns> The value of the first row and first column of the result data table </returns>
public int ReturnZRowZColContentInt(string sql, SqlParameter[] paraList)
{
int ContentInt = 0;
SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
foreach (SqlParameter para in paraList)
{
sqlCommand.Parameters.Add(para);
}
ContentInt = int.Parse(sqlCommand.ExecuteScalar().ToString());
}
catch (Exception xe)
{
throw new Exception(xe.Message);
}
finally
{
this.CloseDbConnection(sqlConnection);
}
return ContentInt;
}
/// <summary>
/// Return and Sql Statement the value of the first row and first column of the corresponding data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraArray"> Parameters of the array </param>
/// <returns> The value of the first row and first column of the result data table </returns>
public override int ReturnZRowZColContentInt(string sql, object[] paraArray)
{
SqlParameter[] paraList = null;
if (paraArray is DbParameterStruct[])
{
paraList = new SqlParameter[paraArray.Length];
for (int iLoop = 0; iLoop < paraArray.Length; iLoop++)
{
DbParameterStruct para = (DbParameterStruct)paraArray[iLoop];
paraList[iLoop] = new SqlParameter("@" + para.ParaName, para.ParaValue);
}
}
else
{
paraList = (SqlParameter[])paraArray;
}
return this.ReturnZRowZColContentInt(sql, paraList);
}
#endregion
#region [ Method ] Batch update database data table
/// <summary>
/// Batch update database data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="TableName"> Data sheet to be updated </param>
public override void UpdateTable(string sql, System.Data.DataTable TableName)
{
SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
sqlDataAdapter.SelectCommand = new SqlCommand(sql, sqlConnection);
sqlDataAdapter.UpdateCommand = sqlCommandBuilder.GetUpdateCommand();
sqlDataAdapter.Update(TableName);
sqlDataAdapter.Dispose();
}
catch (Exception xe)
{
throw new Exception(xe.Message);
}
finally
{
this.CloseDbConnection(sqlConnection);
}
}
#endregion
#region [ Method ] Batch update database data table < Parameter form >
/// <summary>
/// Batch update database data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraList"> Parameters of the array </param>
/// <param name="TableName"> Data sheet to be updated </param>
public void UpdateTable(string sql, SqlParameter[] paraList, System.Data.DataTable TableName)
{
SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
sqlDataAdapter.SelectCommand = new SqlCommand(sql, sqlConnection);
foreach (SqlParameter para in paraList)
{
sqlDataAdapter.SelectCommand.Parameters.Add(para);
}
sqlDataAdapter.UpdateCommand = sqlCommandBuilder.GetUpdateCommand();
sqlDataAdapter.Update(TableName);
sqlDataAdapter.Dispose();
}
catch (Exception xe)
{
throw new Exception(xe.Message);
}
finally
{
this.CloseDbConnection(sqlConnection);
}
}
/// <summary>
/// Batch update database data table
/// </summary>
/// <param name="sql">SQL sentence </param>
/// <param name="paraArray"> Parameters of the array </param>
/// <param name="TableName"> Data sheet to be updated </param>
public override void UpdateTable(string sql, object[] paraArray, DataTable TableName)
{
SqlParameter[] paraList = null;
if (paraArray is DbParameterStruct[])
{
paraList = new SqlParameter[paraArray.Length];
for (int iLoop = 0; iLoop < paraArray.Length; iLoop++)
{
DbParameterStruct para = (DbParameterStruct)paraArray[iLoop];
paraList[iLoop] = new SqlParameter("@" + para.ParaName, para.ParaValue);
}
}
else
{
paraList = (SqlParameter[])paraArray;
}
this.UpdateTable(sql, paraList, TableName);
}
#endregion
#region [ Method ] Command mode batch update database
/// <summary>
/// Command mode batch update database
/// </summary>
/// <param name="insertCommand">InsertCommand command </param>
/// <param name="updateCommand">UpdateCommand command </param>
/// <param name="deleteCommand">DeleteCommand command </param>
/// <param name="TableName"> Data sheet to be updated </param>
/// <returns> Successful execution True: success False: Failure </returns>
public override bool UpdateTableWithCommand(object insertCommand, object updateCommand, object deleteCommand, DataTable TableName)
{
bool IsOk = false;
SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
SqlCommand InsertCommand = null;
SqlCommand UpdateCommand = null;
SqlCommand DeleteCommand = null;
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
// Check Command command
if (insertCommand != null)
{
InsertCommand = (SqlCommand)insertCommand;
InsertCommand.Connection = sqlConnection;
sqlDataAdapter.InsertCommand = InsertCommand;
}
if (updateCommand != null)
{
UpdateCommand = (SqlCommand)updateCommand;
UpdateCommand.Connection = sqlConnection;
sqlDataAdapter.UpdateCommand = UpdateCommand;
}
if (deleteCommand != null)
{
DeleteCommand = (SqlCommand)deleteCommand;
DeleteCommand.Connection = sqlConnection;
sqlDataAdapter.DeleteCommand = DeleteCommand;
}
// Execute the operation command
sqlDataAdapter.Update(TableName);
sqlDataAdapter.Dispose();
IsOk = true;
}
catch (Exception xe)
{
IsOk = false;
throw new Exception(xe.Message);
}
finally
{
this.CloseDbConnection(sqlConnection);
}
return IsOk;
}
#endregion
#region [ Method ] Execute stored procedures < Parameter form >
/// <summary>
/// Execute stored procedures
/// </summary>
/// <param name="StoredProcedureName"> Stored procedure name </param>
/// <param name="paraList"> Parameters of the array </param>
public void ExecuteProcedure(string StoredProcedureName, SqlParameter[] paraList)
{
bool IsHavePara = true;
if (paraList == null || paraList.Length == 0) IsHavePara = false;
SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandType = CommandType.StoredProcedure; // Stored procedure type
sqlCommand.CommandText = StoredProcedureName;
if (IsHavePara)
{
if (paraList == null) return;
for (int iLoop = 0; iLoop < paraList.Length; iLoop++)
{
sqlCommand.Parameters.Add(paraList[iLoop]);
}
}
sqlCommand.ExecuteNonQuery();
}
catch (Exception xe)
{
throw new Exception(xe.Message);
}
finally
{
this.CloseDbConnection(sqlConnection);
}
}
/// <summary>
/// Execute stored procedures
/// </summary>
/// <param name="StoredProcedureName"> Stored procedure name </param>
/// <param name="paraArray"> Parameters of the array </param>
public override void ExecuteProcedure(string StoredProcedureName, object[] paraArray)
{
SqlParameter[] paraList = null;
if (paraArray is DbParameterStruct[])
{
paraList = new SqlParameter[paraArray.Length];
for (int iLoop = 0; iLoop < paraArray.Length; iLoop++)
{
DbParameterStruct para = (DbParameterStruct)paraArray[iLoop];
paraList[iLoop] = new SqlParameter("@" + para.ParaName, para.ParaValue);
}
}
else
{
paraList = (SqlParameter[])paraArray;
}
this.ExecuteProcedure(StoredProcedureName, paraList);
}
#endregion
#region [ Method ] Execute stored procedures < return DataSet>< Parameter form >
/// <summary>
/// Execute stored procedures
/// </summary>
/// <param name="StoredProcedureName"> Stored procedure name </param>
/// <param name="paraList"> Parameters of the array </param>
/// <returns> Result data set </returns>
public DataSet ExecuteProcedureDs(string StoredProcedureName, SqlParameter[] paraList)
{
bool IsHavePara = true;
if (paraList == null || paraList.Length == 0) IsHavePara = false;
SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandType = CommandType.StoredProcedure; // Stored procedure type
sqlCommand.CommandText = StoredProcedureName;
if (IsHavePara)
{
if (paraList == null) return null;
for (int iLoop = 0; iLoop < paraList.Length; iLoop++)
{
sqlCommand.Parameters.Add(paraList[iLoop]);
}
}
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
DataSet ds = new DataSet();
sqlDataAdapter.Fill(ds);
return ds;
}
catch (Exception xe)
{
throw new Exception(xe.Message);
}
finally
{
this.CloseDbConnection(sqlConnection);
}
}
/// <summary>
/// Execute stored procedures
/// </summary>
/// <param name="StoredProcedureName"> Stored procedure name </param>
/// <param name="paraArray"> Parameters of the array </param>
/// <returns> Result data set </returns>
public override DataSet ExecuteProcedureDs(string StoredProcedureName, object[] paraArray)
{
SqlParameter[] paraList = null;
if (paraArray is DbParameterStruct[])
{
paraList = new SqlParameter[paraArray.Length];
for (int iLoop = 0; iLoop < paraArray.Length; iLoop++)
{
DbParameterStruct para = (DbParameterStruct)paraArray[iLoop];
paraList[iLoop] = new SqlParameter("@" + para.ParaName, para.ParaValue);
}
}
else
{
paraList = (SqlParameter[])paraArray;
}
return this.ExecuteProcedureDs(StoredProcedureName, paraList);
}
#endregion
#region [ Method ] Execute a batch and return only those that affect the number of rows SQL sentence < Business >
/// <summary>
/// Execute a batch and return only those that affect the number of rows SQL sentence ( Business )
/// </summary>
/// <param name="sqlList">Sql Statement list </param>
public override void ExecuteSqlWidthTransaction(SqlStruct[] sqlList)
{
SqlConnection sqlConnection = null;
SqlTransaction sqlTransaction = null;
SqlCommand sqlCommand = null;
sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
sqlTransaction = sqlConnection.BeginTransaction();
sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.Transaction = sqlTransaction;
for (int iLoop = 0; iLoop < sqlList.Length; iLoop++)
{
sqlCommand.CommandText = sqlList[iLoop].sql;
if (sqlList[iLoop].para != null)
{
if (sqlList[iLoop].para is DbParameterStruct[])
{
DbParameterStruct[] paraArray = (DbParameterStruct[])sqlList[iLoop].para;
foreach (DbParameterStruct para in paraArray)
{
sqlCommand.Parameters.Add(new SqlParameter("@" + para.ParaName, para.ParaValue));
}
}
else
{
foreach (SqlParameter para in sqlList[iLoop].para)
{
sqlCommand.Parameters.Add(para);
}
}
}
sqlCommand.ExecuteNonQuery();
sqlCommand.Parameters.Clear();
}
sqlTransaction.Commit();
sqlTransaction.Dispose();
}
catch (Exception xe)
{
if (sqlTransaction != null)
{
sqlTransaction.Rollback();
sqlTransaction.Dispose();
}
throw new Exception(xe.Message);
}
finally
{
if (sqlConnection != null) this.CloseDbConnection(sqlConnection);
}
}
/// <summary>
/// Execute a batch and return only those that affect the number of rows SQL sentence ( Business )
/// </summary>
/// <param name="sqlList">Sql Statement list </param>
public override void ExecuteSqlWidthTransaction(List<SqlStruct> sqlList)
{
SqlConnection sqlConnection = null;
SqlTransaction sqlTransaction = null;
SqlCommand sqlCommand = null;
sqlConnection = (SqlConnection)this.OpenDbConnection();
try
{
sqlTransaction = sqlConnection.BeginTransaction();
sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.Transaction = sqlTransaction;
for (int iLoop = 0; iLoop < sqlList.Count; iLoop++)
{
sqlCommand.CommandText = sqlList[iLoop].sql;
if (sqlList[iLoop].para != null)
{
if (sqlList[iLoop].para is DbParameterStruct[])
{
DbParameterStruct[] paraArray = (DbParameterStruct[])sqlList[iLoop].para;
foreach (DbParameterStruct para in paraArray)
{
sqlCommand.Parameters.Add(new SqlParameter("@" + para.ParaName, para.ParaValue));
}
}
else
{
foreach (SqlParameter para in sqlList[iLoop].para)
{
sqlCommand.Parameters.Add(para);
}
}
}
sqlCommand.ExecuteNonQuery();
sqlCommand.Parameters.Clear();
}
sqlTransaction.Commit();
sqlTransaction.Dispose();
}
catch (Exception xe)
{
if (sqlTransaction != null)
{
sqlTransaction.Rollback();
sqlTransaction.Dispose();
}
throw new Exception(xe.Message);
}
finally
{
if (sqlConnection != null) this.CloseDbConnection(sqlConnection);
}
}
#endregion
}
}
边栏推荐
猜你喜欢
Deploy flask project based on LNMP
[reverse primary] Unique
案例:检查空字段【注解+反射+自定义异常】
Flink 解析(四):恢复机制
数据仓库建模使用的模型以及分层介绍
Some feelings of brushing leetcode 300+ questions
学习投资大师的智慧
ByteDance overseas technical team won the championship again: HD video coding has won the first place in 17 items
Coursera cannot play video
Program counter of JVM runtime data area
随机推荐
Logical operation instruction
Interpretation of Flink source code (III): Interpretation of executiongraph source code
唯有學C不負眾望 TOP5 S1E8|S1E9:字符和字符串&&算術運算符
信息与网络安全期末复习(基于老师给的重点)
Learn the wisdom of investment Masters
MySQL basic addition, deletion, modification and query of SQL statements
肖申克的救赎有感
MySQL advanced (index, view, stored procedure, function, password modification)
Prototype chain inheritance
02个人研发的产品及推广-短信平台
Introduction to spring trick of ByteDance: senior students, senior students, senior students, and the author "brocade bag"
Log4j2 major vulnerabilities and Solutions
JVM class loading subsystem
Program counter of JVM runtime data area
Flink parsing (VI): savepoints
Flink 解析(六):Savepoints
JVM运行时数据区之程序计数器
Set up the flutter environment pit collection
IDEA断点调试技巧,多张动图包教包会。
【逆向】脱壳后修复IAT并关闭ASLR