ReadMe.doc - PgFoundry

dinnerattentionΔιαχείριση Δεδομένων

28 Νοε 2012 (πριν από 4 χρόνια και 6 μήνες)

282 εμφανίσεις


1

PostgreSQL Data Access Application Block

5/23/2005



Extending the Data Access Application Block for PostgreSQL


Step 1: Install the prerequisites.


The prerequisites for building the Data Access Application Blocks are:



Nunit (available at
www.nunit.org
)



Npgsql ADO.NET provider (available at
gborg.postgresql.org/
)


Install Nunit and Npgsql before continuing.


Step 2: Rebuilding the Data Access Application Blocks to include su
pport
for the PostgreSQL database type.


The
NpgsqlDatabase.cs

and
NpgsqlCommandWrapper.cs

C# classes
implement the PostgreSQL database type. This implementation relies upon the
Npgsql ADO.NET provider.


Open the Data Access Application Blocks
Data

soluti
on, and add a new project
to the solution.


Hint: If the installation defaults were accepted during installation, then the solution is
located at “C:
\
Program Files
\
Microsoft Enterprise Library
\
src
\
Data.sln”.


Hint: To conform to the existing directory st
ructure, the project should be located at
“C:
\
Program Files
\
Microsoft Enterprise Library
\
src
\
Data
\
Ngsql”. Initially, name the project
“Ngsql”, and then rename the project “Data.Ngsql” after the “Ngsql” folder is created by
the Add New Project dialog.


Nam
e the project
Data.Npgsql
, and set the
Assembly Name

and
Default
Namespace

project properties to
Microsoft.Practices.EnterpriseLibrary.Data.Npgsql
.


Add the
NpgsqlDatabase.cs

and
NpgsqlCommandWrapper.cs

C# classes to
the new project. These C# classes imple
ment the PostgreSQL database type.
This implementation relies upon the Npgsql ADO.NET provider.


Add references to the
Common
,
Configuration
, and
Data

projects to the new
project. Add references to the Nunit (nunit.framework) and PostgreSQL
ADO.NET provide
r (Npgsql) assemblies.



2




Then, rebuild the solution.


Note: If you don’t have the DB2 ADO.NET provider installed, then disable the
corresponding project in the build configuration.




3

Configuring the Data Access Application Block

for PostgreSQL


Create a new database type using the Enterprise Library Configuration tool.
Right
-
click the
Database Types

folder, select
New > Database Type
.




Rename the new database type (i.e. PostgreSQL), and set the database type

Type Name property using the Type Selector. Initially, the PostgreSQL type will
not appear in the list. So, select the
Load an Assembly …

command, and
navigate to the location of the PostgreSQL assembly.





4

Using the Data Access Application Block for Po
stgreSQL


Step 1: Configuring the Application Block


To configure the application block use the
Microsoft patterns & practices >
Enterprise Library > Enterprise Library Configuration

tool. Create a new
application by selecting the
New Application

menu opti
on from the main menu.
Right
-
click the new application folder, and select the
New > Data Access
Application Block

menu option.


Configure a connection string parameter for each parameter in the connection
string (rename the default parameters, and set the

values). These parameters
are concatenated together to form the ultimate connection string. In addition, the
database instance and connection string should be given more meaningful
names.


The XML configuration files must be saved in the application dire
ctory. Select the
Save Application

menu option from the main menu, and select the application
directory.


The configuration tool generates an application configuration file (app.config),
and a separate configuration file for each application block (i.e.
dataConfiguration.config for the Data Access application block). The application
configuration file specifies which application blocks are used, and the location of
the application block configuration file(s). By default, the application blocks
configurati
on file(s) are in the same directory as the application configuration file.


Add the application configuration file to the solution to simplify deployment.


Note: Since the application configuration file (app.config) is copied to the application
directory

during deployment, the application block configuration files must be manually
copied to the application directory during deployment (i.e. before executing the
application using Visual Studio, the application blocks configuration file(s) must be copied
to
the appropriate
bin

directory).



In example, for my application (MyFirstApp) the
app.config

file is renamed
MyFirstApp.exe.config

and copied to the
bin/Debug

directory. So, the
dataConfiguration.config

file must be manually copied to the
bin/Debug

folder.




Step 2: Adding a Reference to the Application Block


To add a reference to the application block, right
-
click the
References

folder in
the solution, and select the
Add References …

menu option. The application
block assemblies do not appear on the
.NE
T

tab of the
Add Reference

dialog.
Select the
Browse …

command in the
Add Reference

dialog, and navigate to

5

the location of the application block assemblies. The application block
assemblies are located in the
bin

directory where the application block is
installed (by default,
C:
\
Program Files
\
Microsoft Enterprise Library
\
bin
). Add
the
Microsoft.Practices.EnterpriseLibrary.Common
,
Microsoft.Practices.EnterpriseLibrary.Configuration
, and
Microsoft.Practices.EnterpriseLibrary.Data

assemblies to the solution.




Step 3: Adding a Reference to the Application Block namespace


Add a reference to the application block namespace.


using Microsoft.Practices.EnterpriseLibrary.Data;




Step 4: Create a Database object


All access to the application block methods is
through a
Database

object.
Therefore, create an instance of the
Database

class using the
DatabaseFactory

class. The
instanceName

parameter corresponds to the name of the Database
Instance configured in step 1.


Database db = DatabaseFactory.CreateDatabase
("myDatabase");



Step 5: Execute the desired Application Block method


Execute the desired database access method through the methods exposed by
the
Database

object.


string sql = "SELECT * FROM MYTABLE";

DataSet ds = db.ExecuteDataSet(CommandType.Text,
sql);


Note: If it is necessary to name the
DataSet

or
DataTable
, then use the
LoadDataSet

method.