214 lines
6.8 KiB
C#
214 lines
6.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Data.Common;
|
|
using System.Windows.Forms;
|
|
|
|
namespace HelperDB
|
|
{
|
|
public class DbHelperSQL
|
|
{
|
|
private static string _ConnectionString = new XmlUse(Application.StartupPath + "\\AIMS.xml").GetNode("ConnectionString")[0].ToString();
|
|
private static SqlConnection mConnection;
|
|
public static SqlConnection Connection
|
|
{
|
|
get
|
|
{
|
|
if (DbHelperSQL.mConnection == null)
|
|
{
|
|
DbHelperSQL.mConnection = new SqlConnection(DbHelperSQL._ConnectionString);
|
|
}
|
|
if (DbHelperSQL.mConnection.State == ConnectionState.Broken)
|
|
{
|
|
DbHelperSQL.mConnection.Close();
|
|
DbHelperSQL.mConnection.Open();
|
|
}
|
|
if (DbHelperSQL.mConnection.State == ConnectionState.Closed)
|
|
{
|
|
DbHelperSQL.mConnection.Open();
|
|
}
|
|
return DbHelperSQL.mConnection;
|
|
}
|
|
}
|
|
private static bool bTransaction;
|
|
private static SqlTransaction trans;
|
|
public static void BeginTrans()
|
|
{
|
|
if (!bTransaction)
|
|
{
|
|
trans = Connection.BeginTransaction();
|
|
bTransaction = true;
|
|
return;
|
|
}
|
|
throw new DbAccessException("The transaction not Commit or Rollback.");
|
|
}
|
|
|
|
public static void CommitTrans()
|
|
{
|
|
if (bTransaction)
|
|
{
|
|
trans.Commit();
|
|
trans = null;
|
|
bTransaction = false;
|
|
return;
|
|
}
|
|
throw new DbAccessException("not in a transaction.");
|
|
}
|
|
|
|
public static void RollbackTrans()
|
|
{
|
|
if (bTransaction)
|
|
{
|
|
trans.Rollback();
|
|
trans = null;
|
|
bTransaction = false;
|
|
return;
|
|
}
|
|
throw new DbAccessException("not in a transaction.");
|
|
}
|
|
|
|
public static 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.SqlException e)
|
|
{
|
|
connection.Close();
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public static bool Exists(string strSql)
|
|
{
|
|
object obj = DbHelperSQL.GetSingle(strSql);
|
|
int cmdresult;
|
|
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
|
{
|
|
cmdresult = 0;
|
|
}
|
|
else
|
|
{
|
|
cmdresult = int.Parse(obj.ToString());
|
|
}
|
|
if (cmdresult == 0)
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public static int ExecNonQuery(string sql)
|
|
{
|
|
using (SqlConnection conn = new SqlConnection(_ConnectionString))
|
|
{
|
|
if (conn.State != ConnectionState.Open)
|
|
conn.Open();
|
|
using (SqlCommand cmd = conn.CreateCommand())
|
|
{
|
|
cmd.Parameters.Clear();
|
|
cmd.CommandText = sql;
|
|
return cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static SqlDataReader GetDataReader(string sql)
|
|
{
|
|
SqlCommand sqlCommand = new SqlCommand(sql, DbHelperSQL.Connection, trans);
|
|
return sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
|
|
}
|
|
|
|
public static SqlDataReader GetDataReader(string sql, params SqlParameter[] values)
|
|
{
|
|
SqlCommand sqlCommand = new SqlCommand(sql, DbHelperSQL.Connection, trans);
|
|
sqlCommand.Parameters.AddRange(values);
|
|
return sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
|
|
}
|
|
|
|
|
|
public static DataTable GetDataTable(string sql)
|
|
{
|
|
using (SqlConnection conn = new SqlConnection(_ConnectionString))
|
|
{
|
|
if (conn.State != ConnectionState.Open)
|
|
conn.Open();
|
|
DataSet ds = new DataSet();
|
|
SqlCommand cmd = new SqlCommand(sql, conn);
|
|
SqlDataAdapter da = new SqlDataAdapter(cmd);
|
|
da.Fill(ds);
|
|
return ds.Tables[0];
|
|
}
|
|
}
|
|
|
|
public static DataSet GetDataSet(string sql)
|
|
{
|
|
using (SqlConnection conn = new SqlConnection(_ConnectionString))
|
|
{
|
|
if (conn.State != ConnectionState.Open)
|
|
conn.Open();
|
|
DataSet ds = new DataSet();
|
|
SqlCommand cmd = new SqlCommand(sql, conn);
|
|
SqlDataAdapter da = new SqlDataAdapter(cmd);
|
|
try
|
|
{
|
|
da.Fill(ds);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
return ds;
|
|
}
|
|
}
|
|
|
|
public static bool ExecuteTrasaction(string sqlStr)
|
|
{
|
|
bool result = true;
|
|
SqlTransaction sqlTransaction = null;
|
|
try
|
|
{
|
|
sqlTransaction = DbHelperSQL.Connection.BeginTransaction("Tran");
|
|
SqlCommand sqlCommand = new SqlCommand(sqlStr, DbHelperSQL.Connection, sqlTransaction);
|
|
sqlCommand.ExecuteNonQuery();
|
|
sqlTransaction.Commit();
|
|
result = true;
|
|
}
|
|
catch
|
|
{
|
|
sqlTransaction.Rollback();
|
|
result = false;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static System.DateTime SystemDate()
|
|
{
|
|
string sql = "select getdate() as SysDate";
|
|
DataTable dataTable = DbHelperSQL.GetDataTable(sql);
|
|
return System.DateTime.Parse(System.DateTime.Parse(dataTable.Rows[0]["SysDate"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
|
|
}
|
|
}
|
|
}
|