如何通过DbProviderFactory 构建DBHelper帮助类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何通过DbProviderFactory 构建DBHelper帮助类相关的知识,希望对你有一定的参考价值。
前一段时间在博客园中找到的一个工厂模式的数据库帮助类,具体是出自哪一位高人记不清楚了,现在Mark一下.1、数据库类型枚举
复制代码
public enum DbProviderType : byte
SqlServer,
mysql,
SQLite,
Oracle,
ODBC,
OleDb,
Firebird,
PostgreSql,
DB2,
Informix,
SqlServerCe
复制代码
2、DbProviderFactory工厂类
复制代码
public class ProviderFactory
private static Dictionary<DbProviderType, string> providerInvariantNames = new Dictionary<DbProviderType, string>();
private static Dictionary<DbProviderType, DbProviderFactory> providerFactoies = new Dictionary<DbProviderType, DbProviderFactory>(20);
static ProviderFactory()
providerInvariantNames.Add(DbProviderType.SqlServer, "System.Data.SqlClient");
providerInvariantNames.Add(DbProviderType.OleDb, "System.Data.OleDb");
providerInvariantNames.Add(DbProviderType.ODBC, "System.Data.ODBC");
providerInvariantNames.Add(DbProviderType.Oracle, "Oracle.DataAccess.Client");
providerInvariantNames.Add(DbProviderType.MySql, "MySql.Data.MySqlClient");
providerInvariantNames.Add(DbProviderType.SQLite, "System.Data.SQLite");
providerInvariantNames.Add(DbProviderType.Firebird, "FirebirdSql.Data.Firebird");
providerInvariantNames.Add(DbProviderType.PostgreSql, "Npgsql");
providerInvariantNames.Add(DbProviderType.DB2, "IBM.Data.DB2.iSeries");
providerInvariantNames.Add(DbProviderType.Informix, "IBM.Data.Informix");
providerInvariantNames.Add(DbProviderType.SqlServerCe, "System.Data.SqlServerCe");
public static string GetProviderInvariantName(DbProviderType providerType)
return providerInvariantNames[providerType];
public static DbProviderFactory GetDbProviderFactory(DbProviderType providerType)
if (!providerFactoies.ContainsKey(providerType))
providerFactoies.Add(providerType, ImportDbProviderFactory(providerType));
return providerFactoies[providerType];
private static DbProviderFactory ImportDbProviderFactory(DbProviderType providerType)
string providerName = providerInvariantNames[providerType];
DbProviderFactory factory = null;
try
factory = DbProviderFactories.GetFactory(providerName);
catch (ArgumentException e)
factory = null;
return factory;
复制代码
3、数据库操作帮助类
复制代码
public sealed class DbHelper
public string ConnectionString get; set;
private DbProviderFactory providerFactory;
public DbHelper(string connectionString, DbProviderType providerType)
ConnectionString = connectionString;
providerFactory = ProviderFactory.GetDbProviderFactory(providerType);
if (providerFactory == null)
throw new ArgumentException("Can\'t load DbProviderFactory for given value of providerType");
********** 具体的ADO.NET操作方法略************
public int ExecuteNonQuery(string sql, IList<DbParameter> parameters)
public DbDataReader ExecuteReader(string sql, IList<DbParameter> parameters)
复制代码
4、数据库配置文件
复制代码
<connectionStrings>
<add name="DARJUAN_DB" connectionString="data source=DARJUAN-PC;database=DARJUAN_DB;user id=***;password=***;connection reset=false;Timeout=120;connection lifetime=120; min pool size=20; max pool size=50" providerName="System.Data.SqlClient" />
</connectionStrings>
复制代码
<system.data>
<DbProviderFactories>
<add name="Odbc Data Provider" invariant="System.Data.Odbc" description=".Net Framework Data Provider for Odbc" type="System.Data.Odbc.OdbcFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add name="OleDb Data Provider" invariant="System.Data.OleDb" description=".Net Framework Data Provider for OleDb" type="System.Data.OleDb.OleDbFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add name="OracleClient Data Provider" invariant="System.Data.OracleClient" description=".Net Framework Data Provider for Oracle" type="System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add name="Microsoft SQL Server Compact Data Provider" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
<add name="Informix Data Provider" invariant="IBM.Data.Informix" description=".Net Framework Data Provider for Informix" type="IBM.Data.Informix.IfxFactory, IBM.Data.Informix" />
<add name="DB2 Data Provider" invariant="IBM.Data.DB2.iSeries" description=".Net Framework Data Provider for DB2 iSeries" type="IBM.Data.DB2.iSeries.DB2Factory, IBM.Data.DB2.iSeries" />
<add name="Firebird Data Provider" invariant="FirebirdSql.Data.FirebirdClient" description="Firebird" type="FirebirdSql.Data.FirebirdClient.FirebirdClientFactory, FirebirdSql.Data.FirebirdClient" />
<add name="Oracle Data Provider" invariant="Oracle.DataAccess.Client" description=".Net Framework Data Provider for Oracle" type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess" />
<add name="PostgreSQL Data Provider Data Provider" invariant="Npgsql" description=".Net Framework Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, System.Data" />
</DbProviderFactories>
</system.data> 参考技术A had the sky been so
通过递归调用构建依赖关系make?
我已经阅读了一些文章,解释了在使用子目录编译项目时递归makefile是如何邪恶的。
但是,我发现了这种自动生成依赖关系的方法
exec = main
objs = main.o A.o B.o C.o # main and object modules
deps = $(objs:.o=.d) # dependencies file
all: $(deps)
$(MAKE) $(exec)
-include $(deps)
%.d: %.c # how to build dependency files
$(CC) -MM $(CFLAGS) $< > $@
main: $(objs) # How to build the main exec
构建目标“all”会根据需要更新依赖项文件,然后在需要时考虑重建主exec。
这种方法有根本缺陷吗?
如果你正在使用GNU make,那么这个hack是不必要的。 GNU make是smart enough自动重建包含的任何(make)文件,然后重启自己:
由于'.d'文件与其他文件一样是makefile,因此make会根据需要重新制作它们,而无需您进一步处理。见Remaking Makefiles。
关于其他make实现,GNU make手册有这样的说法:
对于旧的make程序,传统的做法是使用[-M]编译器功能通过“make depend”之类的命令按需生成先决条件。该命令将创建一个包含所有自动生成的先决条件的文件。然后makefile可以使用include来读取它们(参见Include)。
正如其他贡献者所说,gnu将.d依赖项文件视为Makefile,并在需要时自动重建它们。
这使得递归调用变得不必要,并且问题变得平淡。
当然,像往常一样,“被认为是有害的”纸质书籍必须用一丝盐阅读,如果真的完全阅读,从悠久的传统(克努斯的“反驳”Dijkstra的论文引用Disjktra自己的恐惧被认为是非常教条“和”其他人制造它的宗教“。”狂热的拥护者越过董事会“!)。
以上是关于如何通过DbProviderFactory 构建DBHelper帮助类的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 googleapis Artifacts API 以编程方式查找和删除通过 Google Cloud Build 构建的容器映像?