由于工作需要,从本地Sqlite数据库转为MySql数据库。遇到了一些坑,随后又埋了。记录下过程:
一、安装MySql
首先上官网下载windows版的MySql.解压。详情是参考了几位同鞋的文章:
https://www.cnblogs.com/LxyXY/p/7708016.html MySql 5.7.20安装
http://blog.csdn.net/cdsn_ttk/article/details/78259927 Mysql5.7.20压缩版下载和安装
二、连接MySql
2.1在NuGet管理器中下载MySql.Data,选的是6.9.10版
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.