www.pudn.com > whpower2007-8-24.rar > CallDatabase.cs, change:2007-08-15,size:10362b


using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Data.OleDb; 
using System.Web.UI; 
using System.Configuration; 
 
/*-------------------------------------------------------------------------------------------------------------------------------- 
 *  
 *                                                       用途:数据操作与访问(Access跟SqlServer) 
 *                                                       作者:Stephen·周 
 *                                                       日期:2007-07-07 
 *  
 *--------------------------------------------------------------------------------------------------------------------------------*/ 
namespace com.etong.DBUtility 
{ 
     #region Access通用数据操作 
    public class Access 
    { 
        //private static string connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString; 
 
        private static string connectionString = ConfigurationManager.AppSettings["ProviderName"].ToString() + string.Format("Data Source={0}", System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["FullPath"].ToString())); 
 
        public Access() 
        { 
            // 
            // TODO: 在此处添加构造函数逻辑 
            // 
        } 
 
        /*------------------------------------------------------------------------------------------------------------------------- 
         *  
         *                                                      执行SQL语句 
         *  
         *------------------------------------------------------------------------------------------------------------------------*/ 
        public static bool RunSql(string sql) 
        { 
            OleDbConnection connection = new OleDbConnection(connectionString); 
            OleDbCommand command = new OleDbCommand(); 
 
            if (connection.State != ConnectionState.Open) 
            { 
                connection.Open(); 
            } 
            command.Connection = connection; 
            command.CommandText = sql; 
 
            try 
            { 
                command.ExecuteNonQuery(); 
                return true; 
            } 
            catch 
            { 
                return false; 
            } 
            finally 
            { 
                connection.Close(); 
            } 
        } 
 
        /*------------------------------------------------------------------------------------------------------------------------- 
         *  
         *                                                      执行SQL语句返回结果集 
         *  
         *------------------------------------------------------------------------------------------------------------------------*/ 
        public static DataSet GetDataSet(string sql) 
        { 
            OleDbConnection connection = new OleDbConnection(connectionString); 
            OleDbCommand command = new OleDbCommand(); 
            OleDbDataAdapter dataAdapter; 
            DataSet dataSet = new DataSet(); 
 
            if (connection.State != ConnectionState.Open) 
            { 
                connection.Open(); 
            } 
            command.Connection = connection; 
            command.CommandText = sql; 
            dataAdapter = new OleDbDataAdapter(command); 
 
            try 
            { 
                dataAdapter.Fill(dataSet); 
                return dataSet; 
            } 
            catch 
            { 
                return null; 
            } 
            finally 
            { 
                connection.Close(); 
            } 
        } 
 
        public static DataTable GetDataTable(string sql) 
        { 
            DataSet dataSet = GetDataSet(sql); 
            if (dataSet != null) 
                return GetDataSet(sql).Tables[0]; 
            else 
                return null; 
        } 
 
        public static DataView GetDataView(string sql) 
        { 
            DataTable dataTable = GetDataTable(sql); 
            if (dataTable != null) 
                return GetDataTable(sql).DefaultView; 
            else 
                return null; 
        } 
 
        public static OleDbDataReader GetDataReader(string sql) 
        { 
            OleDbConnection connection = new OleDbConnection(connectionString); 
            OleDbCommand command = new OleDbCommand(); 
            OleDbDataReader dataReader; 
 
            if (connection.State != ConnectionState.Open) 
            { 
                connection.Open(); 
            } 
            command.Connection = connection; 
            command.CommandText = sql; 
 
            try 
            { 
                dataReader = command.ExecuteReader(); 
                return dataReader; 
            } 
            catch 
            { 
                connection.Close(); 
                return null; 
            } 
            finally 
            { 
                //connection.Close(); 
            } 
        } 
    } 
    #endregion 
 
     #region SqlServer通用数据操作 
    public class SqlServer 
    { 
        private static string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; 
 
        public SqlServer() 
        { 
            // 
            // TODO: 在此处添加构造函数逻辑 
            // 
        } 
 
        /*------------------------------------------------------------------------------------------------------------------------- 
         *  
         *                                                      执行SQL语句 
         *  
         *------------------------------------------------------------------------------------------------------------------------*/ 
        public static bool RunSql(string sql) 
        { 
            SqlConnection connection = new SqlConnection(connectionString); 
            SqlCommand command = new SqlCommand(); 
 
            if (connection.State != ConnectionState.Open) 
            { 
                connection.Open(); 
            } 
            command.Connection = connection; 
            command.CommandText = sql; 
 
            try 
            { 
                //command.ExecuteNonQuery(); 
                //return true; 
                //bool a = (command.ExecuteNonQuery() > 0); 
                return (command.ExecuteNonQuery() > 0); 
            } 
            catch 
            { 
                return false; 
            } 
            finally 
            { 
                connection.Close(); 
            } 
        } 
 
        /*------------------------------------------------------------------------------------------------------------------------- 
        *  
        *                                                      执行SQL语句返回结果集 
        *  
        *------------------------------------------------------------------------------------------------------------------------*/ 
        public static DataSet GetDataSet(string sql) 
        { 
            SqlConnection connection = new SqlConnection(connectionString); 
            SqlCommand command = new SqlCommand(); 
            SqlDataAdapter dataAdapter; 
            DataSet dataSet = new DataSet(); 
 
            if (connection.State != ConnectionState.Open) 
            { 
                connection.Open(); 
            } 
            command.Connection = connection; 
            command.CommandText = sql; 
            dataAdapter = new SqlDataAdapter(command); 
 
            try 
            { 
                dataAdapter.Fill(dataSet); 
                return dataSet; 
            } 
            catch 
            { 
                return null; 
            } 
            finally 
            { 
                connection.Close(); 
            } 
        } 
 
        public static DataTable GetDataTable(string sql) 
        { 
            DataSet dataSet = GetDataSet(sql); 
            if (dataSet != null) 
                return dataSet.Tables[0]; 
            else 
                return null; 
        } 
 
        public static DataView GetDataView(string sql) 
        { 
            DataTable dataTable = GetDataTable(sql); 
            if (dataTable != null) 
                return dataTable.DefaultView; 
            else 
                return null; 
        } 
 
        public static SqlDataReader GetDataReader(string sql) 
        { 
            SqlConnection connection = new SqlConnection(connectionString); 
            SqlCommand command = new SqlCommand(); 
            SqlDataReader dataReader; 
 
            if (connection.State != ConnectionState.Open) 
            { 
                connection.Open(); 
            } 
            command.Connection = connection; 
            command.CommandText = sql; 
 
            try 
            { 
                dataReader = command.ExecuteReader(); 
                return dataReader; 
            } 
            catch 
            { 
                connection.Close(); 
                return null; 
            } 
            finally 
            { 
                //connection.Close(); 
            } 
        } 
 
    } 
    #endregion 
 
     #region Web前台通用访问过程 
    public class Common 
    { 
        /*------------------------------------------------------------------------------------------------------------------------- 
         *  
         *                                                      运行客户端脚本 
         *  
         *------------------------------------------------------------------------------------------------------------------------*/ 
        public static void RunScript(Page page, string showMessage) 
        { 
            string script = "<script language='javascript'>alert('" + showMessage + "')</script>"; 
            page.ClientScript.RegisterStartupScript(page.GetType(), "", script); 
        } 
 
        public static void RunScript(Page page, string showMessage, string nextPageUrl) 
        { 
            string script = "<script language='javascript'>alert('" + showMessage + "');window.location.href='" + nextPageUrl + "';</script>"; 
            page.ClientScript.RegisterStartupScript(page.GetType(), "", script); 
        } 
 
        public static void RunScriptString(Page page, string script) 
        { 
            page.ClientScript.RegisterStartupScript(page.GetType(), "", script); 
        } 
    } 
    #endregion 
}