- using Microsoft.VisualBasic;
- using System.Data;
- using System.Data.SqlClient;
- public class SQLBASE
- {
- public static readonly string sqlConnection = System.Configuration.ConfigurationManager.ConnectionStrings("Ccopconnstr").ConnectionString;
- public static SqlConnection conn = new SqlConnection(SQLBASE.sqlConnection);
- public static SqlConnection Db_Conn()
- {
- if ((SQLBASE.conn.State == ConnectionState.Closed)) {
- SQLBASE.conn.Open();
- }
- return SQLBASE.conn;
- }
- public static void close()
- {
- if ((SQLBASE.conn.State != ConnectionState.Closed)) {
- SQLBASE.conn.Close();
- }
- }
- public static void DataBind(GridView MyDataGrid, string CommandTxt)
- {
- try {
- SqlCommand sqlCommd = new SqlCommand(CommandTxt, SQLBASE.conn);
- sqlCommd.CommandType = CommandType.Text;
- SQLBASE.Db_Conn();
- SqlDataReader Dr = sqlCommd.ExecuteReader;
- MyDataGrid.DataSource = Dr;
- MyDataGrid.DataBind();
- Dr.Close();
- sqlCommd.Dispose();
- SQLBASE.close();
- }
- catch (Exception ex) {
- SQLBASE.close();
- throw ex;
- }
- }
- public static void DataBind(DataList MyDataList, string CommandTxt)
- {
- try {
- SqlCommand sqlCommd = new SqlCommand(CommandTxt, SQLBASE.conn);
- sqlCommd.CommandType = CommandType.Text;
- SQLBASE.Db_Conn();
- SqlDataReader Dr = sqlCommd.ExecuteReader;
- MyDataList.DataSource = Dr;
- MyDataList.DataBind();
- Dr.Close();
- sqlCommd.Dispose();
- SQLBASE.close();
- }
- catch (Exception ex) {
- SQLBASE.close();
- throw ex;
- }
- }
- public static void DataBind(Repeater MyRepeater, string CommandTxt)
- {
- try {
- SqlCommand sqlCommd = new SqlCommand(CommandTxt, SQLBASE.conn);
- sqlCommd.CommandType = CommandType.Text;
- Db_Conn();
- SqlDataReader Dr = sqlCommd.ExecuteReader;
- MyRepeater.DataSource = Dr;
- MyRepeater.DataBind();
- Dr.Close();
- sqlCommd.Dispose();
- SQLBASE.close();
- }
- catch (Exception ex) {
- SQLBASE.close();
- throw ex;
- }
- }
- public static void ExecuteNonQuery(string CommandTxt)
- {
- try {
- using (SqlCommand sqlCommd = new SqlCommand(CommandTxt, SQLBASE.conn)) {
- SQLBASE.Db_Conn();
- sqlCommd.ExecuteNonQuery();
- sqlCommd.Dispose();
- }
- }
- catch (Exception ex) {
- throw ex;
- }
- finally {
- SQLBASE.conn.Close();
- }
- }
- public static SqlDataReader ExecuteReader(string CommandTxt)
- {
- SqlDataReader Dr = default(SqlDataReader);
- try {
- SqlCommand sqlCommd = new SqlCommand(CommandTxt, SQLBASE.conn);
- sqlCommd.CommandType = CommandType.Text;
- SQLBASE.Db_Conn();
- Dr = sqlCommd.ExecuteReader;
- sqlCommd.Dispose();
- }
- catch (SqlException e) {
- SQLBASE.conn.Close();
- throw new Exception(e.Message);
- }
- return Dr;
- }
- public object ExecuteScalar(string CommandTxt)
- {
- object obj = null;
- try {
- SQLBASE.Db_Conn();
- using (SqlCommand command = new SqlCommand(CommandTxt, SQLBASE.conn)) {
- obj = command.ExecuteScalar;
- if ((obj == null)) {
- return "";
- }
- }
- }
- catch (Exception Ex) {
- SQLBASE.conn.Close();
- throw Ex;
- }
- return obj;
- }
- public static void ExecuteSqlTran(ArrayList SQLStringList)
- {
- using (SqlConnection conn = new SqlConnection(SQLBASE.sqlConnection)) {
- conn.Open();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = conn;
- SqlTransaction tx = conn.BeginTransaction;
- cmd.Transaction = tx;
- try {
- int n = 0;
- for (n = 0; n <= SQLStringList.Count - 1; n++) {
- string strsql = SQLStringList.Item(n).ToString;
- if ((strsql.Trim.Length > 1)) {
- cmd.CommandText = strsql;
- cmd.ExecuteNonQuery();
- }
- }
- tx.Commit();
- }
- catch (SqlException E) {
- tx.Rollback();
- throw new Exception(E.Message);
- }
- }
- }
- public static DataSet FillDataSet(string sql)
- {
- DataSet ds = new DataSet();
- try {
- Db_Conn();
- SqlCommand sqlCommd = new SqlCommand(sql, conn);
- try {
- SqlDataAdapter adapter = new SqlDataAdapter(sqlCommd);
- adapter.Fill(ds);
- return ds;
- }
- finally {
- ((IDisposable)sqlCommd).Dispose();
- }
- }
- catch (Exception Ex) {
- throw Ex;
- }
- finally {
- conn.Close();
- }
- }
- public static int GetRowsNum(string sqlstr)
- {
- return SQLBASE.FillDataSet(sqlstr).Tables.Item(0).Rows.Count;
- }
- public static bool IfRead(string CommandTxt)
- {
- try {
- if ((SQLBASE.GetRowsNum(CommandTxt) == 0)) {
- return false;
- }
- }
- catch (Exception Ex) {
- throw Ex;
- }
- return true;
- }
- public static object StaticExecuteScalar(string CommandTxt)
- {
- try {
- if (conn.State == ConnectionState.Closed) {
- conn.Open();
- }
- using (SqlCommand command = new SqlCommand(CommandTxt, conn)) {
- object obj = command.ExecuteScalar();
- if (obj == null) {
- return "";
- }
- else {
- return obj;
- }
- }
- }
- catch (Exception Ex) {
- conn.Close();
- throw Ex;
- }
- }
- }