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


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

      设计了VS2010连接SqlServer2008Oracle10gMysql5.524Access2013

    这是前台窗口

    这是后台代码:

    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,查询数据库成功

  • 相关阅读:
    java中如何制作可双击执行的程序--jar打包工具的使用
    GUI菜单——菜单条、菜单、子条目之间关系
    设计模式——单例模式(饿汉式、懒汉式)
    深入理解利用new创建对象的执行过程以Person p=new Person("张三",20);为例
    构造代码块、构造函数、this执行顺序
    OpenGL 动画的制作
    UE4制作插件的插件神器pluginCreator
    3dmax中cs骨骼教程
    如何解决 应用程序无法启动 因为应用程序的并行配置不正确 问题
    如何自学游戏引擎的开发
  • 原文地址:https://www.cnblogs.com/myhappylife/p/3082386.html
Copyright © 2020-2023  润新知