VS2010连接SqlServer2008数据库,Oracle10g数据库,Mysql5.524数据库,Access2013数据库经典教程,不看肯定后悔

  今天看vs2010多态,既然数据库类库都是实现接口的信息,那么我直接使用接口来连接不同数据库,这不是很符合接口的性质哈

  设计了VS2010连接SqlServer2008Oracle10gMysql5.524Access2013

这是前台窗口

VS2010连接SqlServer2008数据库,Oracle10g数据库,Mysql5.524数据库,Access2013数据库经典教程,不看肯定后悔

 

这是后台代码:

using System;

using System.Data;

using System.Configuration;

using System.Windows.Forms;

using System.Data.SqlClient;

using MySql.Data.MySqlClient;

using System.Data.OleDb;

using System.Data.OracleClient;

namespace Main

{

    public partial class Main : Form

    {

        public Main()

        {

            InitializeComponent();

        }



        private void Main_Load(object sender, EventArgs e)

        {

            

        }

        private static string connIStr = null; //存储用户选择的数据库



        private static IDbConnection conn = null;//Connection接口



        private static IDbCommand cmd=null; //Command接口



        private static IDataReader reader;  //读写器接口



        private string queryString = null;//查询命令



        private static string connStr = null; //数据库连接字符串



        private void btnTest_Click(object sender, EventArgs e)

        {

            if (rbAccess.Checked)

            {

                txtQuery.Text = ""; //将TextBox清空,防止数据干扰

                connIStr = "accessConnstr";

                connStr = this.GetConnStr(connIStr);

                conn = new OleDbConnection(connStr);

                queryString = "select * from [User]";

            }

            else if (rbSqlServer.Checked)

            {

                txtQuery.Text = "";

                connIStr = "sqlServerConnstr";

                connStr = this.GetConnStr(connIStr);

                conn = new SqlConnection(connStr);

                queryString = "select * from [User]";

            }

            else if(rbOralce.Checked)

            {

                txtQuery.Text = "";

                connIStr = "oralceConnstr";

                connStr = this.GetConnStr(connIStr);

                conn = new OracleConnection(connStr);

                queryString = "select * from Users";

            }

            

            else if (rbMysql.Checked)

            {

                txtQuery.Text = "";

                connIStr = "mysqlConnstr";

                connStr = this.GetConnStr(connIStr);

                conn = new MySqlConnection(connStr);

                queryString = "select * from User";

                

            }

            this.Connect(conn,cmd,queryString);

        }

        /// <summary>

        /// 进行数据查询

        /// </summary>

        /// <param name="conn"></param>

        /// <param name="cmd"></param>

        /// <param name="reader"></param>

        /// <param name="queryString"></param>

        private void Connect(IDbConnection conn,IDbCommand cmd,string queryString)

        {

            try

            {

                cmd = conn.CreateCommand();

                cmd.CommandText = queryString;//初始化Command对象

                conn.Open(); //打开数据库

                reader = cmd.ExecuteReader();//初始化DataReader对象

                while (reader.Read())

                {

                    //查询单个字段

                    txtQuery.AppendText(reader.GetString(reader.GetOrdinal("UserName"))+"\r\n");

                }

            }

            catch(Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            finally

            {

                conn.Close();//关闭连接

            }

        }

        /// <summary>

        /// 返回查询字符串

        /// </summary>

        /// <param name="connIStr">查询字符串</param>

        /// <returns></returns>

        private string GetConnStr(string connIStr)

        {

            string connStr = ConfigurationManager.ConnectionStrings[connIStr].ConnectionString;

            return connStr;

        }

    }

    

}

配置文件代码:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <connectionStrings>

    <!--Access连接vs2010连接字符串-->

    <add name="accessConnstr" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\visual studio 2010\Project3\多态,接口\vs连接access\bin\Debug\1.accdb" providerName="Access"/>



    <!--Mysql连接vs2010连接字符串-->

    <add name="mysqlConnstr" connectionString="server=localhost;user id=root;Password=****;database=test;persist security info=False" providerName="Mysql"/>

    

    <!--SqlServer连接vs2010连接字符串-->

    <add name="sqlServerConnstr" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Sjms;Integrated Security=True" providerName="SqlServer"/>



    <!--Oracle连接vs2010连接字符串-->

    <add name="oralceConnstr" connectionString="Data Source=Acer-PC;Persist Security Info=True;User ID=****;Password=****" providerName="Oracle"/>

  </connectionStrings>

</configuration>

 

运行一下,Perfact,查询数据库成功

你可能感兴趣的:(sqlserver)