[Switch from csdn] develop multi-database connection classes in factory Mode

Source: Internet
Author: User
Tags class generator oracleconnection
The development of multi-database connection classes in the factory mode is the best application method in the factory mode, and is also involved by many programmers who use the design mode at the beginning. The following is the specific code:
A. Create a class library with the following classes:
1. design an abstract class
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data;
Using system. configuration;

// Connect to the database in factory Mode
Namespace dbaccess
...{
// Factory mode abstract class for Data Connection
Public abstract class dbabstract
...{
// Constructor
Public dbabstract ()
...{
}

// Open a connection
Public abstract void open ();

// Close a connection
Public abstract void close ();

// Public number of data reads
Public abstract void publicclass (string procname, object [] parmas );

// Execute the function without returning Dataset
Public abstract string execsql (string procname, object [] parmas );

// Execute a function that returns dataset.
Public abstract dataset execsqlreturndataset (string tablename, string procname, object [] parmas );

}
}
2. Define an oracle connection class
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data;
Using system. Data. oracleclient;
Using system. configuration;

// Connect to the Oracle database in the database in factory Mode
Namespace dbaccess
...{
// Data connection factory mode Oracle connection class
Internal class dboracle: dbabstract
...{
Private oracleconnection conn = NULL; // data connection
Private oraclecommand cmd = NULL; // connection command

// Constructor
Public dboracle (string constring)
...{
This. Conn = new oracleconnection (constring );
}

// Open a connection
Public override void open ()
...{
If (this. Conn! = NULL & this. Conn. State = connectionstate. Closed)
...{
This. Conn. open ();
}
}

// Close a connection
Public override void close ()
...{
If (this. Conn! = NULL & this. Conn. State = connectionstate. open)
...{
This. Conn. Close ();
}
}

// Public number of data reads
Public override void publicclass (string procname, object [] parmas)
...{
Oracleparameter [] oracleparmas = (oracleparameter []) parmas;
This. cmd = new oraclecommand ();
This. cmd. Connection = This. Conn;
This. cmd. commandtype = commandtype. storedprocedure;
This. cmd. commandtext = procname;
If (this. cmd. Parameters. Count> 0)
...{
This. cmd. Parameters. Clear ();
}
If (oracleparmas! = NULL & oracleparmas. length> 0)
...{
Foreach (oracleparameter P in oracleparmas)
...{
This. cmd. Parameters. Add (P );
}
}
}

// Execute the function without returning Dataset
////Public override string execsql (string procname, object [] parmas)
...{
Try
...{
Oracleparameter [] oracleparmas = (oracleparameter []) parmas;
This. open ();
This. publicclass (procname, oracleparmas );
Int Var = This. cmd. executenonquery ();
This. cmd. Parameters. Clear ();
This. Close ();
Return convert. tostring (VAR );
}
Catch (exception ex)
...{
String E = ex. message;
This. Close ();
Throw;
}
}

// Execute a function that returns dataset.
//////Public override dataset execsqlreturndataset (string tablename, string procname, object [] parmas)
...{
Try
...{
Oracleparameter [] oracleparmas = (oracleparameter []) parmas;
This. publicclass (procname, oracleparmas );
Dataset DS = new dataset ();
Using (oracledataadapter da = new oracledataadapter ())
...{
Da. selectcommand = This. CMD;
Da. Fill (DS, tablename );
}
Return Ds;

}
Catch (exception ex)
...{
String E = ex. message;
Throw;
}
}
}
}
3. Define an sqlserver connection class
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data;
Using system. Data. sqlclient;
Using system. configuration;

// Connect to the sqlserver database in the database in factory Mode
Namespace dbaccess
...{
// Data connection factory mode sqlserver connection class
Internal class dbmssolserver: dbabstract
...{
Private sqlconnection conn = NULL;
Private sqlcommand cmd = NULL;

// Constructor
Public dbmssolserver (string constring)
...{
This. Conn = new sqlconnection (constring );
}

// Open a connection
Public override void open ()
...{
If (this. Conn! = NULL & this. Conn. State = connectionstate. Closed)
...{
This. Conn. open ();
}
}

// Close a connection
Public override void close ()
...{
If (this. Conn! = NULL & this. Conn. State = connectionstate. open)
...{
This. Conn. Close ();
}
}

// Public number of data reads
Public override void publicclass (string procname, object [] parmas)
...{
Sqlparameter [] sqlparmas = (sqlparameter []) parmas;
This. cmd = new sqlcommand ();
This. cmd. Connection = This. Conn;
This. cmd. commandtype = commandtype. storedprocedure;
This. cmd. commandtext = procname;
If (this. cmd. Parameters. Count> 0)
...{
This. cmd. Parameters. Clear ();
}
If (sqlparmas! = NULL & sqlparmas. length> 0)
...{
Foreach (sqlparameter P in sqlparmas)
...{
This. cmd. Parameters. Add (P );
}
}
}

// Execute the function without returning Dataset
Public override string execsql (string procname, object [] parmas)
...{
Try
...{
Sqlparameter [] sqlparmas = (sqlparameter []) parmas;
This. open ();
This. publicclass (procname, sqlparmas );
Int Var = This. cmd. executenonquery ();
This. cmd. Parameters. Clear ();
This. Close ();
Return convert. tostring (VAR );
}
Catch (exception ex)
...{
String E = ex. message;
This. Close ();
Throw;
}
}

// Execute a function that returns dataset.
Public override dataset execsqlreturndataset (string tablename, string procname, object [] parmas)
...{
Try
...{
Sqlparameter [] sqlparmas = (sqlparameter []) parmas;
This. publicclass (procname, sqlparmas );
Dataset DS = new dataset ();
Using (sqldataadapter da = new sqldataadapter ())
...{
Da. selectcommand = This. CMD;
Da. Fill (DS, tablename );
}
Return Ds;

}
Catch (exception ex)
...{
String E = ex. message;
Throw;
}
}
}
}
4. Create a factory
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data;

// Factory class, data connection class Generator
Namespace dbaccess
...{
Public class factoryclass
...{
// Database connection factory Generator
//Public dbabstract getdb (string constring, string mydbtype)
...{
If (mydbtype = "oracle ")
...{
Return new dboracle (constring );
}
Else if (mydbtype = "MSSQLServer ")
...{
Return new dbmssolserver (constring );
}
Else if (mydbtype = "access ")
...{
Return new dbmsaccess (constring );
}
Else
...{
Return NULL;
}
}
}
}
B. Generate a solution to generate a DLL
C. reference the DLL generated above in another project
D. Use DLL to connect to the database and input data
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Data. oracleclient;

Namespace featureinfo
...{
Public partial class frmread: Form
...{
Public frmread ()
...{
Initializecomponent ();
}

Private dbaccess. factoryclass FAC = NULL;
Private dbaccess. dbabstract DBA = NULL;

Private void frmread_load (Object sender, eventargs E)
...{
FAC = new dbaccess. factoryclass ();

DBA = FAC. getdb (system. configuration. configurationsettings. deleettings ["oracle"], "oracle ");
}

Private void getclient (mapxlib. Feature FTR) // this parameter is available because it is developed for MapX.
...{
Mapxlib. Feature fftr = FTR; // defines a MapX object.
String ftrid = fftr. keyValue; // Value
Map. Layers [This. layername]. keyfield = "equipid ";
Mapxlib. Feature eftr = FTR;
String eqid = eftr. keyValue;
Map. Layers [This. layername]. keyfield = "shortname ";
Mapxlib. Feature sftr = FTR;
String shortname = sftr. keyValue;
Map. Layers [This. layername]. keyfield = "ftrid ";

// Create an oracleparameter Array
Oracleparameter [] parmas = new oracleparameter [3]... {
New oracleparameter ("nftr", oracletype. varchar, 13 ),
New oracleparameter ("neqid", oracletype. varchar, 50 ),
New oracleparameter ("nshortname", oracletype. varchar, 14)
};
If (ftrid! = NULL)
...{
Parmas [0]. value = ftrid;
}
Else
...{
Parmas [0]. value = dbnull. value;
}
If (eqid! = NULL)
...{
Parmas [1]. value = eqid;
}
Else
...{
Parmas [1]. value = dbnull. value;
}
If (shortname! = NULL)
...{
Parmas [2]. value = shortname;
}
Else
...{
Parmas [2]. value = dbnull. value;
}

String T = DBA. execsql ("pro_insertclient", (object []) parmas );
// Execute the input operation and forcibly convert the oracleparameter array into an object array as a parameter to pass in the function
}
}
}

Because the current requirement is only input data, only one example of executing the insert stored procedure is written. Next time, the example returned from the dataset C # connected to the Oracle database for query execution will be provided.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.