C#连接MySQL

图片 1

  由于职业亟待,从本地Sqlite数据库转为MySql数据库。境遇了意气风发部分坑,随后又埋了。记录下过程:

 一、安装MySql

  首先上官方网址下载windows版的MySql.解压。详细的情况是参照他事他说加以考察了二位同鞋的小说:

         MySql
5.7.20安装

     
   Mysql5.7.20压缩版下载和安装

 二、连接MySql

     2.1在NuGet微处理器中下载MySql.Data,选的是6.9.10

   
  图片 1

 

    2.2
由于以前用的是DbConnection等少年老成连串的类,由此只需改下数据库连接与调度下SQL语句便能够切换数据库了

    

public static DbConnection CreateConnection()
        {
            if (string.IsNullOrEmpty(DbHelper.dbProviderName))
                dbProviderName = "MySql.Data.MySqlClient";
            DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbHelper.dbProviderName);
            DbConnection dbconn = dbfactory.CreateConnection();
            dbconn.ConnectionString = DbHelper.dbConnectionString;
            return dbconn;
        }

   2.3 上面所聊起的坑就是假诺NeGet中选的本子是6.10.5的话。以下语句中DataTable重返的是NULL.

      public DataTable ExecuteDataTable(DbCommand cmd)
        {
            DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbHelper.dbProviderName);
            DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
            dbDataAdapter.SelectCommand = cmd;
            DataTable dataTable = new DataTable();
            dbDataAdapter.Fill(dataTable);
            return dataTable;
        }

 经苦苦寻觅,开掘原本是此版本的MySql.Data.Dll的Buy!

over.

 

Leave a Comment.