Although many of my friends are using. Net for data access, there should be few DB2 users. Occasionally, ODBC or ole db is used.
DB2 9.0 + provides ADO. net provider that supports. NET 2.0. Especially for friends who use purexml to develop XQuery, this function is richer than ole db and ODBC.
- I installed DB2 9.5 Express-C.
- The development environment is vs 2008, but it is more convenient to use the DB 2005 Visual Studio 9.5 add-ins of VS 2005.
Based onCodeThe access is not mentioned. Here we will introduce a method of APP. config (Google does not find a suitable method, and providername = "IBM. Data. DB2" is hit by mistake ")
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>
/// Abstract base class for completing data operations
/// </Summary>
Public Class Database
{
Protected String Name;
Connectionstringsettings setting;
PublicDatabase (StringName)
{
This. Name=Name;
This. Setting=Configurationmanager. connectionstrings [name];
}
/// <Summary>
/// Dbconnection instance
/// </Summary>
/// <Returns> </returns>
Public Dbconnection getconnection ()
{
Return Createconnection ( This . Name );
}
# RegionHelper Methods
/// <Summary>
/// Create a dbconnection instance based on the configuration
/// </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 = 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 >
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. Data;
Namespace Marvellousxml. Library. Data
{
/// <Summary>
/// Construct the factory type of the database (unfinished blank)
/// </Summary>
Public Static Class Databasefactory
{
/// <Summary>
/// Structure
/// </Summary>
/// <Param name = "name"> Logical connection name </Param>
/// <Returns> Database instance </Returns>
Public Static Database create ( String Name)
{
Return New Database (name );
}
}
}