为了适应应对各种数据库间切换,我们有时想要要自定义写一个通用的SqlHelper。常用的做法就是在
System.Data.Common.DbProviderFactories.GetFactory(dbProviderName).CreateConnection ,来返回对应的 数据库的Connection 。
直接上测试源码:
本文转载自:https://www.gylmap.com
string dbProviderName = "System.Data.Odbc"; dbProviderName = "System.Data.OleDb"; dbProviderName = "System.Data.SqlClient"; dbProviderName = "System.Data.OracleClient"; //引用 System.Data.SQLite.dll dbProviderName = "System.Data.SQLite"; //引用 MySql.Data.dll dbProviderName = "MySql.Data.MySqlClient"; //引用 Oracle.ManagedDataAccess.dll dbProviderName = "Oracle.ManagedDataAccess.Client"; DbProviderFactory dbfactory = DbProviderFactories.GetFactory(dbProviderName); DbConnection dbconn = dbfactory.CreateConnection; dbconn.ConnectionString = string.Empty;// 数据库连接字符串
需要注意的 config 配置:
最后附上代码。