IBM 为 DB2 9.5 提供的 ADO.NET Provider
虽然很多朋友都在用.NET作数据访问,但使用DB2的应该不多,偶尔使用也是用ODBC或OLE DB方式。
DB2 9.0+提供了支持.NET 2.0的ADO.NET Provider, 尤其对于使用PureXML开发XQuery的朋友而言,这个功能上要比OLE DB,ODBC的方式丰富。
- 我安装的是DB2 9.5 Express-C版本
- 开发环境是VS 2008,不过其实用VS 2005的DB 9.5 Visual Studio 2005 Add-ins更方便
基于代码的访问就不提了,这里介绍一个App.Config的方式 (google了一下,找不到合适的,经过误打误撞碰出了providerName="IBM.Data.DB2")
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.Data.Common;
using System.Configuration;
namespace MarvellousXml.Library.Data
{
/// <summary>
/// 完成数据操作的抽象基类
/// </summary>
public class Database
{
protected string name;
ConnectionStringSettings setting;
public Database(string name)
{
this.name = name;
this.setting = ConfigurationManager.ConnectionStrings[name];
}
/// <summary>
/// 提供DbConnection实例
/// </summary>
/// <returns></returns>
public DbConnection GetConnection()
{
return CreateConnection(this.name);
}
#region Helper Methods
/// <summary>
/// 根据配置构造DbConnection实例
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private DbConnection CreateConnection(string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name");
DbProviderFactory factory = DbProviderFactories.GetFactory(setting.ProviderName);
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = setting.ConnectionString;
return connection;
}
#endregion
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.Data.Common;
using System.Configuration;
namespace MarvellousXml.Library.Data
{
/// <summary>
/// 完成数据操作的抽象基类
/// </summary>
public class Database
{
protected string name;
ConnectionStringSettings setting;
public Database(string name)
{
this.name = name;
this.setting = ConfigurationManager.ConnectionStrings[name];
}
/// <summary>
/// 提供DbConnection实例
/// </summary>
/// <returns></returns>
public DbConnection GetConnection()
{
return CreateConnection(this.name);
}
#region Helper Methods
/// <summary>
/// 根据配置构造DbConnection实例
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private DbConnection CreateConnection(string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name");
DbProviderFactory factory = DbProviderFactories.GetFactory(setting.ProviderName);
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = setting.ConnectionString;
return connection;
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using System.Data.Common;
using MarvellousXml.Library.Data;
namespace MarvellousXml.Library.Data.Test
{
[TestClass]
public class DatabaseFixture
{
[TestMethod]
public void CreateDb2Database()
{
string dbName = "DB2.Test";
Database database = DatabaseFactory.Create(dbName);
Assert.IsNotNull(database);
DbConnection connection = database.GetConnection();
connection.Open();
Assert.IsTrue(connection.State == ConnectionState.Open);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using System.Data.Common;
using MarvellousXml.Library.Data;
namespace MarvellousXml.Library.Data.Test
{
[TestClass]
public class DatabaseFixture
{
[TestMethod]
public void CreateDb2Database()
{
string dbName = "DB2.Test";
Database database = DatabaseFactory.Create(dbName);
Assert.IsNotNull(database);
DbConnection connection = database.GetConnection();
connection.Open();
Assert.IsTrue(connection.State == ConnectionState.Open);
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="DB2.Test" connectionString="Database=SAMPLE;User ID=...;Server=127.0.0.1; Password = ...; Persist Security Info=True" providerName="IBM.Data.DB2"/>
</connectionStrings>
</configuration>
<configuration>
<connectionStrings>
<add name="DB2.Test" connectionString="Database=SAMPLE;User ID=...;Server=127.0.0.1; Password = ...; Persist Security Info=True" providerName="IBM.Data.DB2"/>
</connectionStrings>
</configuration>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace MarvellousXml.Library.Data
{
/// <summary>
/// 构造Database的工厂类型 (还没有完成的 毛坯)
/// </summary>
public static class DatabaseFactory
{
/// <summary>
/// 构造
/// </summary>
/// <param name="name">逻辑连接名称</param>
/// <returns>Database实例</returns>
public static Database Create(string name)
{
return new Database(name);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace MarvellousXml.Library.Data
{
/// <summary>
/// 构造Database的工厂类型 (还没有完成的 毛坯)
/// </summary>
public static class DatabaseFactory
{
/// <summary>
/// 构造
/// </summary>
/// <param name="name">逻辑连接名称</param>
/// <returns>Database实例</returns>
public static Database Create(string name)
{
return new Database(name);
}
}
}
贸易电子化,技术全球化