Oracle跳数据库连接类(System.Data.Common)

a.net为为们们提供了逐一数据库的链接.
比如说MSSQL就是System.Data.SqlClient
         Oracle就是System.Data.OracleClient
         MySql就是MySql.Data.MySqlClient
其他的应和就是的了.还有啊DB2.还有等等一些.
以前我们举行越数据库的早晚用之是反射.只不过看到网上广大讲评说反射效率不怎么好
就此近年来在研System.Data.Common
否便是通用的数目链接类吧

自然这里只是做测试.没有举行最多的包,
此间是自身的目录结构
Oracle 1

话不多说.上代码
理所当然首先步还是于web.config里面写及多少链接语句.
此地是最好要的
本身这里测试的只有mysql,mssql

Oracle 2    <connectionStrings>
Oracle 3        <add name=”ConnectionString1″ connectionString=”Data Source=LIUJU;Initial Catalog=MSPetShop4;Persist Security Info=True;User ID=sa;Password=***” providerName=”System.Data.SqlClient”/>
Oracle 4        <add name=”ConnectionString” connectionString=”server=localhost;user id=root;password=***;persist security info=True;database=mspetshop4″ providerName=”MySql.Data.MySqlClient”/>
Oracle 5    </connectionStrings>

此的name你协调安装一个名字就可以了.connectionString就是数据库链接语句.这里没有啊好说的
最好重点的哪怕是背后的providerName这里虽是描写及而数据库要引入的名称空间.这样程序才见面分晓您用之是啊数据库

脚这里我就算是摹写的一个webConfig的援助类.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace DBHelper.Library
{
    public static class WebConfigHelper
    {
        private readonly static string DBConnectionString = ConfigurationManager.ConnectionStrings[“ConnectionString”].ConnectionString;
        private readonly static string DBProviderName = ConfigurationManager.ConnectionStrings[“ConnectionString”].ProviderName;
        public static string GetConnectionString
        {
            get { return DBConnectionString; }
        }
        public static string GetProviderName
        {
            get { return DBProviderName; }
        }
    }
}

好.下面就是重点的了

using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DBHelper.Library
{
    public static class DataAccessHelper
    {
        /// <summary>
        /// 创建DbCommand对象
        /// </summary>
        /// <returns>DbCommand对象</returns>
        public static DbCommand GetCommand()
        {
            //得到webConfig里面的若引用的号空间
            string DBProviderName = WebConfigHelper.GetProviderName;
            //得到webConfig里面的链接字符串
            string DBConnectionString = WebConfigHelper.GetConnectionString;

            //数据库工厂类为夫数据库创建一个数据库链接对象
            DbProviderFactory dpf = DbProviderFactories.GetFactory(DBProviderName);
            //创建Connection
            DbConnection conn = dpf.CreateConnection();
            conn.ConnectionString = DBConnectionString;
            //创建Command
            DbCommand comm = conn.CreateCommand();
            comm.CommandType = CommandType.Text;
            return comm;
        }

        /// <summary>
        /// 执行查询,返回datatable
        /// </summary>
        /// <param name=”command”></param>
        /// <returns></returns>
        public static DataTable ExecuteSelectCommand(DbCommand command)
        {
            DataTable table;
            try
            {
                if (command.Connection.State != ConnectionState.Open)
                {
                    command.Connection.Open();
                }
                DbDataReader reader = command.ExecuteReader();
                table = new DataTable();
                table.Load(reader);
                reader.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
            finally
            {
                command.Connection.Close();
            }
            return table;

        }
        /// <summary>
        /// 执行update insert del操作
        /// </summary>
        /// <param name=”command”></param>
        /// <returns>返回影响行数</returns>
        public static int ExecuteNonQuery(DbCommand command)
        {
            int affectRows = -1;
            try
            {
                if (command.Connection.State != ConnectionState.Open)
                {
                    command.Connection.Open();
                }
                affectRows = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
            finally
            {
                command.Connection.Close();
            }
            return affectRows;
        }
        /// <summary>
        /// 返回第一排第一履行
        /// </summary>
        /// <param name=”command”></param>
        /// <returns></returns>
        public static string ExecuteScalar(DbCommand command)
        {
            string value = “”;
            try
            {
                if (command.Connection.State != ConnectionState.Open)
                {
                    command.Connection.Open();
                }
                value = command.ExecuteScalar().ToString();

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
            finally
            {
                command.Connection.Close();
            }
            return value;
        }
    }
}

这样你就是可假设配置一下webConfig程序就算自行知道您链接的是呀数据库,
就得自动为汝创造一个数据库链接对象了.

采取方式

        protected void Page_Load(object sender, EventArgs e)
        {
            DbCommand dc = DataAccessHelper.GetCommand();
            dc.CommandText = “SELECT ProductId, CategoryId, Name, Descn FROM Product”;
            DataTable dt = DataAccessHelper.ExecuteSelectCommand(dc);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

运作结果
Oracle 6
鲜只数据库都测试正常

参考网址
http://www.cnblogs.com/freegarden/archive/2009/09/25/1574044.html
这以下载地址
http://files.cnblogs.com/liuju150/DBHelper20091008124542.zip

相关文章