年度变更建库软件5.0版本
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.

419 lines
16 KiB

6 months ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
namespace Kingo.Plugin.ShapeToKOApp.Helper
{
public static class SQLiteHelper
{
public static int ExecuteNonQuery(string connectionString, SQLiteCommand cmd)
{
int result = 0;
if (connectionString == null || connectionString.Length == 0)
{
throw new ArgumentNullException("connectionString");
}
using (SQLiteConnection sqliteConnection = new SQLiteConnection(connectionString))
{
SQLiteTransaction sqliteTransaction = null;
SQLiteHelper.PrepareCommand(cmd, sqliteConnection, ref sqliteTransaction, true, cmd.CommandType, cmd.CommandText, new SQLiteParameter[0]);
try
{
result = cmd.ExecuteNonQuery();
sqliteTransaction.Commit();
}
catch (Exception ex)
{
sqliteTransaction.Rollback();
throw ex;
}
}
return result;
}
public static int ExecuteNonQuery(string connectionString, string commandText, CommandType commandType)
{
int result = 0;
if (connectionString == null || connectionString.Length == 0)
{
throw new ArgumentNullException("connectionString");
}
if (commandText == null || commandText.Length == 0)
{
throw new ArgumentNullException("commandText");
}
SQLiteCommand sqliteCommand = new SQLiteCommand();
using (SQLiteConnection sqliteConnection = new SQLiteConnection(connectionString))
{
SQLiteTransaction sqliteTransaction = null;
SQLiteHelper.PrepareCommand(sqliteCommand, sqliteConnection, ref sqliteTransaction, true, commandType, commandText, new SQLiteParameter[0]);
try
{
result = sqliteCommand.ExecuteNonQuery();
sqliteTransaction.Commit();
}
catch (Exception ex)
{
sqliteTransaction.Rollback();
throw ex;
}
}
return result;
}
public static int ExecuteNonQuery(string connectionString, string commandText, CommandType commandType, params SQLiteParameter[] cmdParms)
{
int result = 0;
if (connectionString == null || connectionString.Length == 0)
{
throw new ArgumentNullException("connectionString");
}
if (commandText == null || commandText.Length == 0)
{
throw new ArgumentNullException("commandText");
}
SQLiteCommand sqliteCommand = new SQLiteCommand();
using (SQLiteConnection sqliteConnection = new SQLiteConnection(connectionString))
{
SQLiteTransaction sqliteTransaction = null;
SQLiteHelper.PrepareCommand(sqliteCommand, sqliteConnection, ref sqliteTransaction, true, commandType, commandText, new SQLiteParameter[0]);
try
{
result = sqliteCommand.ExecuteNonQuery();
sqliteTransaction.Commit();
}
catch (Exception ex)
{
sqliteTransaction.Rollback();
throw ex;
}
}
return result;
}
public static object ExecuteScalar(string connectionString, SQLiteCommand cmd)
{
object result = 0;
if (connectionString == null || connectionString.Length == 0)
{
throw new ArgumentNullException("connectionString");
}
using (SQLiteConnection sqliteConnection = new SQLiteConnection(connectionString))
{
SQLiteTransaction sqliteTransaction = null;
SQLiteHelper.PrepareCommand(cmd, sqliteConnection, ref sqliteTransaction, true, cmd.CommandType, cmd.CommandText, new SQLiteParameter[0]);
try
{
result = cmd.ExecuteScalar();
sqliteTransaction.Commit();
}
catch (Exception ex)
{
sqliteTransaction.Rollback();
throw ex;
}
}
return result;
}
public static object ExecuteScalar(string connectionString, string commandText, CommandType commandType)
{
object result = 0;
if (connectionString == null || connectionString.Length == 0)
{
throw new ArgumentNullException("connectionString");
}
if (commandText == null || commandText.Length == 0)
{
throw new ArgumentNullException("commandText");
}
SQLiteCommand sqliteCommand = new SQLiteCommand();
using (SQLiteConnection sqliteConnection = new SQLiteConnection(connectionString))
{
SQLiteTransaction sqliteTransaction = null;
SQLiteHelper.PrepareCommand(sqliteCommand, sqliteConnection, ref sqliteTransaction, true, commandType, commandText, new SQLiteParameter[0]);
try
{
result = sqliteCommand.ExecuteScalar();
sqliteTransaction.Commit();
}
catch (Exception ex)
{
sqliteTransaction.Rollback();
throw ex;
}
}
return result;
}
public static object ExecuteScalar(string connectionString, string commandText, CommandType commandType, params SQLiteParameter[] cmdParms)
{
object result = 0;
if (connectionString == null || connectionString.Length == 0)
{
throw new ArgumentNullException("connectionString");
}
if (commandText == null || commandText.Length == 0)
{
throw new ArgumentNullException("commandText");
}
SQLiteCommand sqliteCommand = new SQLiteCommand();
using (SQLiteConnection sqliteConnection = new SQLiteConnection(connectionString))
{
SQLiteTransaction sqliteTransaction = null;
SQLiteHelper.PrepareCommand(sqliteCommand, sqliteConnection, ref sqliteTransaction, true, commandType, commandText, new SQLiteParameter[0]);
try
{
result = sqliteCommand.ExecuteScalar();
sqliteTransaction.Commit();
}
catch (Exception ex)
{
sqliteTransaction.Rollback();
throw ex;
}
}
return result;
}
public static DbDataReader ExecuteReader(string connectionString, SQLiteCommand cmd)
{
DbDataReader result = null;
if (connectionString == null || connectionString.Length == 0)
{
throw new ArgumentNullException("connectionString");
}
SQLiteConnection conn = new SQLiteConnection(connectionString);
SQLiteTransaction sqliteTransaction = null;
SQLiteHelper.PrepareCommand(cmd, conn, ref sqliteTransaction, false, cmd.CommandType, cmd.CommandText, new SQLiteParameter[0]);
try
{
result = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw ex;
}
return result;
}
public static DbDataReader ExecuteReader(string connectionString, string commandText, CommandType commandType)
{
DbDataReader result = null;
if (connectionString == null || connectionString.Length == 0)
{
throw new ArgumentNullException("connectionString");
}
if (commandText == null || commandText.Length == 0)
{
throw new ArgumentNullException("commandText");
}
SQLiteConnection conn = new SQLiteConnection(connectionString);
SQLiteCommand sqliteCommand = new SQLiteCommand();
SQLiteTransaction sqliteTransaction = null;
SQLiteHelper.PrepareCommand(sqliteCommand, conn, ref sqliteTransaction, false, commandType, commandText, new SQLiteParameter[0]);
try
{
result = sqliteCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw ex;
}
return result;
}
public static DbDataReader ExecuteReader(string connectionString, string commandText, CommandType commandType, params SQLiteParameter[] cmdParms)
{
DbDataReader result = null;
if (connectionString == null || connectionString.Length == 0)
{
throw new ArgumentNullException("connectionString");
}
if (commandText == null || commandText.Length == 0)
{
throw new ArgumentNullException("commandText");
}
SQLiteConnection conn = new SQLiteConnection(connectionString);
SQLiteCommand sqliteCommand = new SQLiteCommand();
SQLiteTransaction sqliteTransaction = null;
SQLiteHelper.PrepareCommand(sqliteCommand, conn, ref sqliteTransaction, false, commandType, commandText, cmdParms);
try
{
result = sqliteCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw ex;
}
return result;
}
public static DataSet ExecuteDataSet(string connectionString, SQLiteCommand cmd)
{
DataSet dataSet = new DataSet();
SQLiteConnection conn = new SQLiteConnection(connectionString);
SQLiteTransaction sqliteTransaction = null;
SQLiteHelper.PrepareCommand(cmd, conn, ref sqliteTransaction, false, cmd.CommandType, cmd.CommandText, new SQLiteParameter[0]);
try
{
SQLiteDataAdapter sqliteDataAdapter = new SQLiteDataAdapter(cmd);
sqliteDataAdapter.Fill(dataSet);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (cmd.Connection != null)
{
if (cmd.Connection.State == ConnectionState.Open)
{
cmd.Connection.Close();
}
}
}
return dataSet;
}
public static DataSet ExecuteDataSet(string connectionString, string commandText, CommandType commandType)
{
if (connectionString == null || connectionString.Length == 0)
{
throw new ArgumentNullException("connectionString");
}
if (commandText == null || commandText.Length == 0)
{
throw new ArgumentNullException("commandText");
}
DataSet dataSet = new DataSet();
SQLiteConnection sqliteConnection = new SQLiteConnection(connectionString);
SQLiteCommand cmd = new SQLiteCommand();
SQLiteTransaction sqliteTransaction = null;
SQLiteHelper.PrepareCommand(cmd, sqliteConnection, ref sqliteTransaction, false, commandType, commandText, new SQLiteParameter[0]);
try
{
SQLiteDataAdapter sqliteDataAdapter = new SQLiteDataAdapter(cmd);
sqliteDataAdapter.Fill(dataSet);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (sqliteConnection != null)
{
if (sqliteConnection.State == ConnectionState.Open)
{
sqliteConnection.Close();
}
}
}
return dataSet;
}
public static DataSet ExecuteDataSet(string connectionString, string commandText, CommandType commandType, params SQLiteParameter[] cmdParms)
{
if (connectionString == null || connectionString.Length == 0)
{
throw new ArgumentNullException("connectionString");
}
if (commandText == null || commandText.Length == 0)
{
throw new ArgumentNullException("commandText");
}
DataSet dataSet = new DataSet();
SQLiteConnection sqliteConnection = new SQLiteConnection(connectionString);
SQLiteCommand cmd = new SQLiteCommand();
SQLiteTransaction sqliteTransaction = null;
SQLiteHelper.PrepareCommand(cmd, sqliteConnection, ref sqliteTransaction, false, commandType, commandText, cmdParms);
try
{
SQLiteDataAdapter sqliteDataAdapter = new SQLiteDataAdapter(cmd);
sqliteDataAdapter.Fill(dataSet);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (sqliteConnection != null)
{
if (sqliteConnection.State == ConnectionState.Open)
{
sqliteConnection.Close();
}
}
}
return dataSet;
}
public static DataTable SelectPaging(string connString, string tableName, string strColumns, string strWhere, string strOrder, int pageSize, int currentIndex, out int recordOut)
{
DataTable dataTable = new DataTable();
recordOut = Convert.ToInt32(SQLiteHelper.ExecuteScalar(connString, "select count(*) from " + tableName, CommandType.Text));
string format = "select {0} from {1} where {2} order by {3} limit {4} offset {5} ";
int num = (currentIndex - 1) * pageSize;
string commandText = string.Format(format, new object[]
{
strColumns,
tableName,
strWhere,
strOrder,
pageSize.ToString(),
num.ToString()
});
using (DbDataReader dbDataReader = SQLiteHelper.ExecuteReader(connString, commandText, CommandType.Text))
{
if (dbDataReader != null)
{
dataTable.Load(dbDataReader);
}
}
return dataTable;
}
private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, ref SQLiteTransaction trans, bool useTrans, CommandType cmdType, string cmdText, params SQLiteParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (useTrans)
{
trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);
cmd.Transaction = trans;
}
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SQLiteParameter parameter in cmdParms)
{
cmd.Parameters.Add(parameter);
}
}
}
}
}