using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System;public class WDABase : System.IDisposable
{
    public string SQL;
    public string SQLClStr;
    public SqlConnection SQLConnectionStr;
    public SqlCommand SQLCommand;
    public SqlDataAdapter SQLDataAdapter;
    public SqlDataReader SQLDataReader;
    public string MyConString = "Data Source=EsI;Initial Catalog=LkDBM;User ID=sa;Password=abcd1234a/";
    public string MySevers = "EsI";
    public string MyDBMS = "LkDBM";
    public string MyLogin = "sa";
    public string MyPW = "abcd1234a/";
    public string MyTestConStr;    // 检测冗余的调用 
    private bool disposedValue = false;    // IDisposable 
    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposedValue)
        {
            if (disposing)
            {
                // TODO: 显式调用时释放非托管资源 
                if ((SQLConnectionStr != null))
                {
                    SQLConnectionStr.Close();
                    SQLConnectionStr.Dispose();
                    SQLConnectionStr = null;
                }
                if ((SQLDataReader != null))
                {
                    SQLDataReader.Close();
                    SQLDataReader = null;
                }
                if ((SQLDataAdapter != null))
                {
                    SQLDataAdapter.Dispose();
                    SQLDataAdapter = null;
                }
                if ((SQLCommand != null))
                {
                    SQLCommand.Dispose();
                    SQLCommand = null;
                }
            }
            // TODO: 释放共享的非托管资源 
        }
        this.disposedValue = true;
    }    public WDABase()
    {
        MyTestConStr = "Data Source=" + MySevers + ";" + "Initial Catalog=" + MyDBMS + ";" + "User ID=" + MyLogin + ";" + "Password=" + MyPW;
        SQLConnectionStr = new SqlConnection(MyTestConStr);
    }    public void SQLOpenConnection()
    {
        try
        {
            SQLConnectionStr.Open();
        }        catch (System.Exception OleDBExceptionErr)
        {
            throw new System.Exception(OleDBExceptionErr.Message, OleDBExceptionErr.InnerException);        }
    }    public void SQLCloseConnection()
    {
        SQLConnectionStr.Close();
    }    public void InitializeSQLCommand()
    {
        if (SQLCommand == null)
        {
            try
            {
                SQLCommand = new SqlCommand(SQL, SQLConnectionStr);
                if (!SQL.ToUpper().StartsWith("SELECT") & !SQL.ToUpper().StartsWith("INSERT") & !SQL.ToUpper().StartsWith("UPDATE") & !SQL.ToUpper().StartsWith("DELECT"))
                {
                    SQLCommand.CommandType = CommandType.StoredProcedure;                }
            }
            catch (System.Exception OleDBExceptionErr)
            {
                throw new System.Exception(OleDBExceptionErr.Message, OleDBExceptionErr.InnerException);
            }        }
    }     
    public void SQLAddParameter(string Name, SqlDbType Type, int Size, object Value)
    {
        try
        {
            SQLCommand.Parameters.Add(Name, Type, Size).Value = Value;
        }
        catch (SqlException OleDbExceptionErr)
        {
            throw new System.Exception(OleDbExceptionErr.Message, OleDbExceptionErr.InnerException);        }
    }    public void InitializeSQLDataAdapter()
    {
        try
        {
            SQLDataAdapter = new SqlDataAdapter();
            SQLDataAdapter.SelectCommand = SQLCommand;
        }        catch (SqlException OleDbExceptionErr)
        {
            throw new System.Exception(OleDbExceptionErr.Message, OleDbExceptionErr.InnerException);        }
    }    public void FillSQLDataSet(ref DataSet oDataSet, string TableName)
    {
        try
        {
            InitializeSQLCommand();
            InitializeSQLDataAdapter();
            SQLDataAdapter.Fill(oDataSet, TableName);
        }
        catch (System.Exception OleDBExceptionErr)
        {
            throw new System.Exception(OleDBExceptionErr.Message, OleDBExceptionErr.InnerException);        }
        SQLCommand.Dispose();
        SQLCommand = null;
        SQLDataAdapter.Dispose();
        SQLDataAdapter = null;
    }    public void FillsqlDataTable(ref DataTable oDatatable)
    {
        try
        {
            InitializeSQLCommand();
            InitializeSQLDataAdapter();
            SQLDataAdapter.Fill(oDatatable);
        }
        catch (System.Exception OleDBExceptionErr)
        {
            throw new System.Exception(OleDBExceptionErr.Message, OleDBExceptionErr.InnerException);
        }
        SQLCommand.Dispose();
        SQLCommand = null;
        SQLDataAdapter.Dispose();
        SQLDataAdapter = null;    }    public int SQLExecuteStoredProcedure()
    {
        int functionReturnValue = 0;
        try
        {
            SQLOpenConnection();
            functionReturnValue = SQLCommand.ExecuteNonQuery();
        }
        catch (System.Exception OleDBExceptionErr)
        {
            throw new System.Exception(OleDBExceptionErr.Message, OleDBExceptionErr.InnerException);
        }
        finally
        {
            SQLCloseConnection();
        }
        return functionReturnValue;
    }    public int SQLTLWDExecuteStoredProcedure()
    {
        int functionReturnValue = 0;
        try
        {            functionReturnValue = SQLCommand.ExecuteNonQuery();
        }
        catch (System.Exception OleDBExceptionErr)
        {
            throw new System.Exception(OleDBExceptionErr.Message, OleDBExceptionErr.InnerException);
        }
        return functionReturnValue;
    }
    #region " IDisposable Support "
    // Visual Basic 添加此代码是为了正确实现可处置模式。 
    public void Dispose()
    {
        // 不要更改此代码。请将清理代码放入上面的 Dispose(ByVal disposing As Boolean) 中。 
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    #endregion}