CallDatabase.cs
上传用户:tiancihang
上传日期:2014-03-12
资源大小:21387k
文件大小:10k
源码类别:

.net编程

开发平台:

C#

  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Data.OleDb;
  5. using System.Web.UI;
  6. using System.Configuration;
  7. /*--------------------------------------------------------------------------------------------------------------------------------
  8.  * 
  9.  *                                                       用途:数据操作与访问(Access跟SqlServer)
  10.  *                                                       作者:Stephen·周
  11.  *                                                       日期:2007-07-07
  12.  * 
  13.  *--------------------------------------------------------------------------------------------------------------------------------*/
  14. namespace com.etong.DBUtility
  15. {
  16.      #region Access通用数据操作
  17.     public class Access
  18.     {
  19.         //private static string connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
  20.         private static string connectionString = ConfigurationManager.AppSettings["ProviderName"].ToString() + string.Format("Data Source={0}", System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["FullPath"].ToString()));
  21.         public Access()
  22.         {
  23.             //
  24.             // TODO: 在此处添加构造函数逻辑
  25.             //
  26.         }
  27.         /*-------------------------------------------------------------------------------------------------------------------------
  28.          * 
  29.          *                                                      执行SQL语句
  30.          * 
  31.          *------------------------------------------------------------------------------------------------------------------------*/
  32.         public static bool RunSql(string sql)
  33.         {
  34.             OleDbConnection connection = new OleDbConnection(connectionString);
  35.             OleDbCommand command = new OleDbCommand();
  36.             if (connection.State != ConnectionState.Open)
  37.             {
  38.                 connection.Open();
  39.             }
  40.             command.Connection = connection;
  41.             command.CommandText = sql;
  42.             try
  43.             {
  44.                 command.ExecuteNonQuery();
  45.                 return true;
  46.             }
  47.             catch
  48.             {
  49.                 return false;
  50.             }
  51.             finally
  52.             {
  53.                 connection.Close();
  54.             }
  55.         }
  56.         /*-------------------------------------------------------------------------------------------------------------------------
  57.          * 
  58.          *                                                      执行SQL语句返回结果集
  59.          * 
  60.          *------------------------------------------------------------------------------------------------------------------------*/
  61.         public static DataSet GetDataSet(string sql)
  62.         {
  63.             OleDbConnection connection = new OleDbConnection(connectionString);
  64.             OleDbCommand command = new OleDbCommand();
  65.             OleDbDataAdapter dataAdapter;
  66.             DataSet dataSet = new DataSet();
  67.             if (connection.State != ConnectionState.Open)
  68.             {
  69.                 connection.Open();
  70.             }
  71.             command.Connection = connection;
  72.             command.CommandText = sql;
  73.             dataAdapter = new OleDbDataAdapter(command);
  74.             try
  75.             {
  76.                 dataAdapter.Fill(dataSet);
  77.                 return dataSet;
  78.             }
  79.             catch
  80.             {
  81.                 return null;
  82.             }
  83.             finally
  84.             {
  85.                 connection.Close();
  86.             }
  87.         }
  88.         public static DataTable GetDataTable(string sql)
  89.         {
  90.             DataSet dataSet = GetDataSet(sql);
  91.             if (dataSet != null)
  92.                 return GetDataSet(sql).Tables[0];
  93.             else
  94.                 return null;
  95.         }
  96.         public static DataView GetDataView(string sql)
  97.         {
  98.             DataTable dataTable = GetDataTable(sql);
  99.             if (dataTable != null)
  100.                 return GetDataTable(sql).DefaultView;
  101.             else
  102.                 return null;
  103.         }
  104.         public static OleDbDataReader GetDataReader(string sql)
  105.         {
  106.             OleDbConnection connection = new OleDbConnection(connectionString);
  107.             OleDbCommand command = new OleDbCommand();
  108.             OleDbDataReader dataReader;
  109.             if (connection.State != ConnectionState.Open)
  110.             {
  111.                 connection.Open();
  112.             }
  113.             command.Connection = connection;
  114.             command.CommandText = sql;
  115.             try
  116.             {
  117.                 dataReader = command.ExecuteReader();
  118.                 return dataReader;
  119.             }
  120.             catch
  121.             {
  122.                 connection.Close();
  123.                 return null;
  124.             }
  125.             finally
  126.             {
  127.                 //connection.Close();
  128.             }
  129.         }
  130.     }
  131.     #endregion
  132.      #region SqlServer通用数据操作
  133.     public class SqlServer
  134.     {
  135.         private static string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
  136.         public SqlServer()
  137.         {
  138.             //
  139.             // TODO: 在此处添加构造函数逻辑
  140.             //
  141.         }
  142.         /*-------------------------------------------------------------------------------------------------------------------------
  143.          * 
  144.          *                                                      执行SQL语句
  145.          * 
  146.          *------------------------------------------------------------------------------------------------------------------------*/
  147.         public static bool RunSql(string sql)
  148.         {
  149.             SqlConnection connection = new SqlConnection(connectionString);
  150.             SqlCommand command = new SqlCommand();
  151.             if (connection.State != ConnectionState.Open)
  152.             {
  153.                 connection.Open();
  154.             }
  155.             command.Connection = connection;
  156.             command.CommandText = sql;
  157.             try
  158.             {
  159.                 //command.ExecuteNonQuery();
  160.                 //return true;
  161.                 //bool a = (command.ExecuteNonQuery() > 0);
  162.                 return (command.ExecuteNonQuery() > 0);
  163.             }
  164.             catch
  165.             {
  166.                 return false;
  167.             }
  168.             finally
  169.             {
  170.                 connection.Close();
  171.             }
  172.         }
  173.         /*-------------------------------------------------------------------------------------------------------------------------
  174.         * 
  175.         *                                                      执行SQL语句返回结果集
  176.         * 
  177.         *------------------------------------------------------------------------------------------------------------------------*/
  178.         public static DataSet GetDataSet(string sql)
  179.         {
  180.             SqlConnection connection = new SqlConnection(connectionString);
  181.             SqlCommand command = new SqlCommand();
  182.             SqlDataAdapter dataAdapter;
  183.             DataSet dataSet = new DataSet();
  184.             if (connection.State != ConnectionState.Open)
  185.             {
  186.                 connection.Open();
  187.             }
  188.             command.Connection = connection;
  189.             command.CommandText = sql;
  190.             dataAdapter = new SqlDataAdapter(command);
  191.             try
  192.             {
  193.                 dataAdapter.Fill(dataSet);
  194.                 return dataSet;
  195.             }
  196.             catch
  197.             {
  198.                 return null;
  199.             }
  200.             finally
  201.             {
  202.                 connection.Close();
  203.             }
  204.         }
  205.         public static DataTable GetDataTable(string sql)
  206.         {
  207.             DataSet dataSet = GetDataSet(sql);
  208.             if (dataSet != null)
  209.                 return dataSet.Tables[0];
  210.             else
  211.                 return null;
  212.         }
  213.         public static DataView GetDataView(string sql)
  214.         {
  215.             DataTable dataTable = GetDataTable(sql);
  216.             if (dataTable != null)
  217.                 return dataTable.DefaultView;
  218.             else
  219.                 return null;
  220.         }
  221.         public static SqlDataReader GetDataReader(string sql)
  222.         {
  223.             SqlConnection connection = new SqlConnection(connectionString);
  224.             SqlCommand command = new SqlCommand();
  225.             SqlDataReader dataReader;
  226.             if (connection.State != ConnectionState.Open)
  227.             {
  228.                 connection.Open();
  229.             }
  230.             command.Connection = connection;
  231.             command.CommandText = sql;
  232.             try
  233.             {
  234.                 dataReader = command.ExecuteReader();
  235.                 return dataReader;
  236.             }
  237.             catch
  238.             {
  239.                 connection.Close();
  240.                 return null;
  241.             }
  242.             finally
  243.             {
  244.                 //connection.Close();
  245.             }
  246.         }
  247.     }
  248.     #endregion
  249.      #region Web前台通用访问过程
  250.     public class Common
  251.     {
  252.         /*-------------------------------------------------------------------------------------------------------------------------
  253.          * 
  254.          *                                                      运行客户端脚本
  255.          * 
  256.          *------------------------------------------------------------------------------------------------------------------------*/
  257.         public static void RunScript(Page page, string showMessage)
  258.         {
  259.             string script = "<script language='javascript'>alert('" + showMessage + "')</script>";
  260.             page.ClientScript.RegisterStartupScript(page.GetType(), "", script);
  261.         }
  262.         public static void RunScript(Page page, string showMessage, string nextPageUrl)
  263.         {
  264.             string script = "<script language='javascript'>alert('" + showMessage + "');window.location.href='" + nextPageUrl + "';</script>";
  265.             page.ClientScript.RegisterStartupScript(page.GetType(), "", script);
  266.         }
  267.         public static void RunScriptString(Page page, string script)
  268.         {
  269.             page.ClientScript.RegisterStartupScript(page.GetType(), "", script);
  270.         }
  271.     }
  272.     #endregion
  273. }