asp.net DbProviderFactory的使用-示例
程序员文章站
2024-03-09 13:13:41
复制代码 代码如下: using system; using system.data; using system.configuration; using system.w...
复制代码 代码如下:
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using crystaldecisions.crystalreports.engine;
using crystaldecisions.shared;
using system.data.common;
/// <summary>
/// orderinfoconfiguration 的摘要说明
/// </summary>
public class orderinfoconfiguration
{
private const string connectionstring=@"provider=microsoft.jet.oledb.4.0;data source=c:\program files\tutorialsamplecodeprojects\xtreme.mdb";
private const string querystring="select a.[order date] as 订单时间,a.[order id] as 订单号,b.[last name]+\" \"+b.[first name] as 员工姓名,c.[customer name] as 客户姓名,d.[product name] as 产品名 from orders a,employee b,customer c,product d ,[orders detail] e where a.[employee id]=b.[employee id] and a.[customer id]=c.[customer id] and a.[order id]=e.[order id] and e.[product id]=d.[product id]";
public static dataset orderinfodataset
{
get
{
dataset dataset = new dataset();
dbproviderfactory factory = dbproviderfactories.getfactory("system.data.oledb"); //获取工厂
dbconnection con = factory.createconnection();//创建连接
con.connectionstring = connectionstring;
dbcommand cmd = factory.createcommand();//创建命令
cmd.commandtext = querystring;
cmd.commandtype = commandtype.text;
cmd.connection = con;
dbdataadapter dapter = factory.createdataadapter();//创建适配器
dapter.selectcommand = cmd;
dapter.fill(dataset);//填充
return dataset;
}
}
public orderinfoconfiguration()
{
//
// todo: 在此处添加构造函数逻辑
//
}
}
dbproviderfactory factory = dbproviderfactories.getfactory("system.data.oledb"); //获取工厂 这句就可以获得一个工厂,用这个工厂就可发生产该数据提供程序的各种对象了。
如果是连接
sqlserver:dbproviderfactory factory = dbproviderfactories.getfactory("system.data.sqlclient");
oracle:dbproviderfactory factory = dbproviderfactories.getfactory("system.data.oracleclient");
odbc:dbproviderfactory factory = dbproviderfactories.getfactory("system.data.odbc");
使用了这个,要是想换个数据库,是不是很方便了呢?呵呵。