首页 > 代码库 > Abstract Factory(抽象工厂模式)
Abstract Factory(抽象工厂模式)
System.Data.Common.DbProviderFactory
public abstract class DbProviderFactory
{
public abstract class DbProviderFactory
{
public virtual DbConnection CreateConnection()
{
return null;
}
public virtual DbCommand CreateCommand()
{
return null;
}
}
System.Data.SqlClient.SqlClientFactory
public sealed class SqlClientFactory : DbProviderFactory, IServiceProvider
{
public override DbConnection CreateConnection()
{
return new SqlConnection();
}
public override DbCommand CreateCommand()
{
return new SqlCommand();
}
}
System.Data.Odbc.OdbcFactory
public sealed class OdbcFactory : DbProviderFactory
{
public override DbConnection CreateConnection()
{
return new OdbcConnection();
}
public override DbCommand CreateCommand()
{
return new OdbcCommand();
}
}
System.Data.Common.DbConnection
public abstract class DbConnection : Component, IDbConnection
{
}
System.Data.SqlClient.SqlConnection
public sealed partial class SqlConnection : DbConnection, ICloneable
{
}
System.Data.Odbc.OdbcConnection
public sealed partial class OdbcConnection : DbConnection, ICloneable
{
}
System.Data.Common.DbCommand
public abstract class DbCommand : Component, IDbCommand
{
}
System.Data.SqlClient.SqlCommand
public sealed class SqlCommand : DbCommand, ICloneable
{
}
System.Data.Odbc.OdbcCommand
public sealed class OdbcCommand : DbCommand, ICloneable
{
}
public class Client
{
public static void Main()
{
{
public static void Main()
{
string connectionString =
System.Configuration.ConfigurationManager.AppSettings["DB_CONNECTION_STRING"];
string providerName =
System.Configuration.ConfigurationManager.AppSettings["DB_PROVIDER_NAME"];
DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = connectionString;
using (connection)
{
DbCommand command = factory.CreateCommand();
command.CommandText =
"INSERT INTO t_users(user_username, user_password) VALUES(‘admin‘ ,‘admin))";
command.Connection = connection;
command.ExecuteNonQuery();
}
}
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。