A portable database

Source: Internet
Author: User
Tags oracleconnection connectionstrings

The original Published time: 2008-08-02--from my Baidu article [imported by moving tools]

Web. config inside:

<?xml version= "1.0"?>
<configuration>
<appSettings>
<add key= "DbType" value= "Access"/>
<!--here the optional value must be SQL Server or Access or oracle-->
</appSettings>
<connectionStrings>
<add name= "Tempconn" connectionstring= "Data source=thc\sqlexpress;initial catalog=temp; Persist Security info=true; User id=thc123_com; Password=thc123_net "providername=" System.Data.SqlClient "/>
<add name= "Nameconn" connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=| Datadirectory|\name.mdb; Persist Security info=true "providername=" System.Data.OleDb "/>
</connectionStrings>

--------------------------------------------------------

Fdatatype class

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 System.Data.OleDb;
Using System.Data.SqlClient;
Using System.Data.OracleClient; I don't have Oracle on my machine, I can't show you, but the principle is the same.

<summary>
Hiding details about the instance of creating a specific type (from a particular data provider) to the higher level of the application can now interact with the data source using the general behavior exposed through the base interface.
The Sky wears www.thc123.com
</summary>

Namespace SAO
{//Define a namespace
<summary>
The set of database names to migrate to
</summary>
public enum DatabaseType
{//Declaration of an enumeration type, the enumeration parameter is the set of database names we may be porting to
Sql server
Access,
Oracle
。。。 Other database names that may be used
}
<summary>
Type set of common parameters
</summary>
public enum ParameterType
{//define a type set of common parameters for an enumeration type
Integer,
Char,
VarChar
}
public class Fdatatype
{
Public Fdatatype ()
{
//
TODO: Add constructor logic here
//
}
<summary>
Create and initialize a connection connection instance
</summary>
<param name= "DbType" > Data provider Type </param>
<returns> Returns a database connection instance that has been created </returns>
public static IDbConnection createconnection (DatabaseType dbType)
{
IDbConnection con; Declare a IDbConnection instance
Switch (dbType)
{//Based on enumeration values to determine what type of database connection instance to create
Case Databasetype.access:
con = new OleDbConnection (configurationmanager.connectionstrings["Nameconn"). ConnectionString);
Break
Case Databasetype.oracle:
con = new OracleConnection (configurationmanager.connectionstrings["Oracleconn"). ConnectionString);
Break
Default
con = new SqlConnection (configurationmanager.connectionstrings["Tempconn"). ConnectionString);
Break
}
return con;
}
<summary>
Create and initialize a command instance
</summary>
<param name= "QueryString" >sql query statement or stored procedure name </param>
<param name= "DbType" > Data provider Type </param>
<param name= "Con" > Database Connection Instance </param>
<returns> Returns a command instance object that has already been created </returns>
public static IDbCommand CreateCommand (String QueryString, DatabaseType dbtype,idbconnection con)
{
IDbCommand cmd;
Switch (dbType)
{
Case Databasetype.access:
cmd = new OleDbCommand (QueryString, (OleDbConnection) con);
Note (OleDbConnection) con), this section represents the argument that the DB connection instance is coerced into type XXX type
Break
Case Databasetype.oracle:
cmd = new OracleCommand (QueryString, (oracleconnection) con);
Break
Default
cmd = new SqlCommand (QueryString, (SqlConnection) con);
Break
}
return cmd;
}
<summary>
Create and initialize a DataAdapter object
</summary>
<param name= "cmd" >command instance object </param>
<param name= "DbType" > Data provider Type </param>
<returns> returns a DataAdapter object that has been created </returns>
public static IDbDataAdapter Createdataadapter (IDbCommand cmd, databasetype dbType)
{
IDbDataAdapter Ida;
Switch (dbType)
{
Case Databasetype.access:
Ida = new OleDbDataAdapter ((OleDbCommand) cmd);
Break
Case Databasetype.oracle:
Ida = new OracleDataAdapter ((OracleCommand) cmd);
Break
Default
Ida=new SqlDataAdapter ((SqlCommand) cmd);
Break
}
return Ida;
}
}
}
-----------------------------------------

Fdataset class

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;

Namespace SAO
{
<summary>
Use which class we created earlier to establish a connection to the database and get the data
</summary>
public class Fdataset
{
DatabaseType DbType = (databasetype) enum.parse (typeof (DatabaseType), configurationsettings.appsettings["DbType"]);
IDbConnection Mycon;
DataSet ds;

Public Fdataset ()
{
Mycon = Fdatatype. CreateConnection (DbType);
ds = new DataSet ();
}

Get the database provider that we set up in the Web. config file
Public DataSet Tds ()
{
IDbCommand mycmd = Fdatatype. CreateCommand ("SELECT * from Name", DbType, mycon);
IDbDataAdapter Myda = Fdatatype. Createdataadapter (myCMD, DbType);
Myda. Fill (DS);
return DS;
}
}
}

A portable database

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.