
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;

namespace DataPumpWindowsService
{
	public class SQLHelper
	{
        private const int outTime = 360;
		private string m_sMessage = "";
		public SqlConnection m_conMS_SQL;
		public string Message
		{
			get
			{
				return this.m_sMessage;
			}
		}

        public SQLHelper(string connectstring)
        {
            this.m_conMS_SQL = new SqlConnection(connectstring);
        }

		public bool OpenConnection()
		{
			bool result;
			if (this.m_conMS_SQL.State == ConnectionState.Open)
			{
				result = true;
			}
			else
			{
				try
				{
					this.m_conMS_SQL.Open();
				}
				catch (Exception ex)
				{
					this.m_sMessage = ex.Message;
					result = false;
					return result;
				}
				if (this.m_conMS_SQL.State != ConnectionState.Open)
				{
					this.m_sMessage = "打开连接失败";
					result = false;
				}
				else
				{
					result = true;
				}
			}
			return result;
		}
		public bool CloseConnection()
		{
			bool result;
			if (this.m_conMS_SQL.State == ConnectionState.Closed)
			{
				result = true;
			}
			else
			{
				try
				{
					this.m_conMS_SQL.Close();
				}
				catch (Exception ex)
				{
					this.m_sMessage = ex.Message;
					result = false;
					return result;
				}
				if (this.m_conMS_SQL.State != ConnectionState.Closed)
				{
					this.m_sMessage = "关闭连接失败";
					result = false;
				}
				else
				{
					result = true;
				}
			}
			return result;
		}
		public bool ExecuteNonQuery(string p_sCommand)
		{
			bool result;
			if (!this.OpenConnection())
			{
				result = false;
			}
			else
			{
				try
				{
					SqlCommand sqlCommand = this.m_conMS_SQL.CreateCommand();
                    sqlCommand.CommandTimeout = 360;
					sqlCommand.CommandText = p_sCommand;
					sqlCommand.ExecuteNonQuery();
				}
				catch (Exception ex)
				{
					this.m_sMessage = ex.Message;
					this.CloseConnection();
					result = false;
					return result;
				}
				this.CloseConnection();
				result = true;
			}
			return result;
		}
		public object ExecuteResult(string p_sCommand)
		{
			object obj = null;
			object result;
			if (!this.OpenConnection())
			{
				result = obj;
			}
			else
			{
				try
				{
					SqlCommand sqlCommand = this.m_conMS_SQL.CreateCommand();
                    sqlCommand.CommandTimeout = 360;
					sqlCommand.CommandText = p_sCommand;
					obj = sqlCommand.ExecuteScalar();
				}
				catch (Exception ex)
				{
					this.m_sMessage = ex.Message;
				}
				finally
				{
					this.CloseConnection();
				}
				result = obj;
			}
			return result;
		}
		public DataTable ExecuteResultTable(string p_sCommand)
		{
			DataTable dataTable = null;
			DataTable result;
			if (!this.OpenConnection())
			{
				result = dataTable;
			}
			else
			{
				try
				{
					dataTable = new DataTable();
					SqlCommand sqlCommand = this.m_conMS_SQL.CreateCommand();
                    sqlCommand.CommandTimeout = 360;
					sqlCommand.CommandText = p_sCommand;
					sqlCommand.ExecuteNonQuery();
					new SqlDataAdapter
					{
						SelectCommand = sqlCommand
					}.Fill(dataTable);
				}
				catch (Exception ex)
				{
					this.m_sMessage = ex.Message;
				}
				finally
				{
					this.CloseConnection();
				}
				result = dataTable;
			}
			return result;
		}
		public bool ExecuteSqls(ArrayList p_oSqls)
		{
			bool result;
			if (!this.OpenConnection())
			{
				result = false;
			}
			else
			{
				try
				{
					SqlCommand sqlCommand = this.m_conMS_SQL.CreateCommand();
                    sqlCommand.CommandTimeout = 360;
					for (int i = 0; i < p_oSqls.Count; i++)
					{
						sqlCommand.CommandText = p_oSqls[i].ToString();
						sqlCommand.ExecuteNonQuery();
					}
				}
				catch (Exception ex)
				{
					this.m_sMessage = ex.Message;
					result = false;
					return result;
				}
				finally
				{
					this.CloseConnection();
				}
				result = true;
			}
			return result;
		}
		public bool ExecuteSqlsByAffair(ArrayList p_oSqls)
		{
			bool result;
			if (!this.OpenConnection())
			{
				result = false;
			}
			else
			{
				SqlTransaction sqlTransaction = this.m_conMS_SQL.BeginTransaction();
				try
				{
					SqlCommand sqlCommand = this.m_conMS_SQL.CreateCommand();
                    sqlCommand.CommandTimeout = 360;
					sqlCommand.Transaction = sqlTransaction;
					for (int i = 0; i < p_oSqls.Count; i++)
					{
						sqlCommand.CommandText = p_oSqls[i].ToString();
						sqlCommand.ExecuteNonQuery();
					}
					sqlTransaction.Commit();
				}
				catch (Exception ex)
				{
					sqlTransaction.Rollback();
					this.m_sMessage = ex.Message;
					result = false;
					return result;
				}
				finally
				{
					this.CloseConnection();
				}
				result = true;
			}
			return result;
		}
		public DataTable GetDataTable(string p_sCommand)
		{
			DataTable result;
			if (!this.OpenConnection())
			{
				result = null;
			}
			else
			{
				try
				{
					DataTable dataTable = new DataTable();
					SqlCommand sqlCommand = this.m_conMS_SQL.CreateCommand();
					sqlCommand.CommandTimeout = 360;
					sqlCommand.CommandText = p_sCommand;
					SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
					sqlDataAdapter.Fill(dataTable);
					result = dataTable;
				}
				catch (Exception ex)
				{
					this.m_sMessage = ex.Message;
					result = null;
				}
				finally
				{
					this.CloseConnection();
				}
			}
			return result;
		}
		public DataTable GetDataTable(string p_sCommand, string p_sTableName)
		{
			DataTable result;
			if (!this.OpenConnection())
			{
				result = null;
			}
			else
			{
				try
				{
					DataTable dataTable = new DataTable();
					SqlCommand sqlCommand = this.m_conMS_SQL.CreateCommand();
                    sqlCommand.CommandTimeout = 360;
					sqlCommand.CommandText = p_sCommand;
					SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
					sqlDataAdapter.Fill(dataTable);
					dataTable.TableName = p_sTableName;
					result = dataTable;
				}
				catch (Exception ex)
				{
					this.m_sMessage = ex.Message;
					result = null;
				}
				finally
				{
					this.CloseConnection();
				}
			}
			return result;
		}
		public DataTable GetDataTable(string p_sCommand, string p_sTableName, int p_iCurrentPage, int p_iPageSize)
		{
			DataTable result;
			if (!this.OpenConnection())
			{
				result = null;
			}
			else
			{
				try
				{
					DataSet dataSet = new DataSet();
					SqlCommand sqlCommand = this.m_conMS_SQL.CreateCommand();
                    sqlCommand.CommandTimeout = 360;
					sqlCommand.CommandText = p_sCommand;
					SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
					sqlDataAdapter.Fill(dataSet, (p_iCurrentPage - 1) * p_iPageSize, p_iPageSize, p_sTableName);
					result = dataSet.Tables[0];
				}
				catch (Exception ex)
				{
					this.m_sMessage = ex.Message;
					result = null;
				}
				finally
				{
					this.CloseConnection();
				}
			}
			return result;
		}
		public DataSet GetDataSet(string p_sCommand)
		{
			DataSet result;
			if (!this.OpenConnection())
			{
				result = null;
			}
			else
			{
				try
				{
					DataSet dataSet = new DataSet();
					SqlCommand sqlCommand = this.m_conMS_SQL.CreateCommand();
                    sqlCommand.CommandTimeout = 360;
					sqlCommand.CommandText = p_sCommand;
					SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
					sqlDataAdapter.Fill(dataSet);
					result = dataSet;
				}
				catch (Exception ex)
				{
					this.m_sMessage = ex.Message;
					result = null;
				}
				finally
				{
					this.CloseConnection();
				}
			}
			return result;
		}
		public bool RunSqlScript(string s_Command)
		{
			bool result;
			if (!this.OpenConnection())
			{
				result = false;
			}
			else
			{
				if (s_Command == string.Empty)
				{
					result = false;
				}
				else
				{
					string[] array = Regex.Split(s_Command + " ", "[ |\r\n]go[ |\r\n]", RegexOptions.IgnoreCase);
					SqlCommand sqlCommand = this.m_conMS_SQL.CreateCommand();
                    sqlCommand.CommandTimeout = 360;
					SqlTransaction sqlTransaction = this.m_conMS_SQL.BeginTransaction();
					sqlCommand.Connection = this.m_conMS_SQL;
					sqlCommand.Transaction = sqlTransaction;
					try
					{
						string[] array2 = array;
						for (int i = 0; i < array2.Length; i++)
						{
							string text = array2[i];
							if (text.Trim().Length > 0)
							{
								sqlCommand.CommandText = text;
								sqlCommand.ExecuteNonQuery();
							}
						}
						sqlTransaction.Commit();
					}
					catch (Exception ex)
					{
						this.m_sMessage = ex.Message;
						sqlTransaction.Rollback();
						result = false;
						return result;
					}
					finally
					{
						sqlTransaction.Dispose();
						this.CloseConnection();
					}
					result = true;
				}
			}
			return result;
		}
	}
}
