Connection strings for Access databases and database operations classes

Source: Internet
Author: User

<!--Access database connection mode--
<add name= "qytangconnectionstring" connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=| datadirectory| Test.mdb "providername=" System.Data.OleDb "/>

Using System;
Using System.Data;
Using System.Data.OleDb;
Using System.Configuration;

Namespace Qytang.gengerate
{
<summary>
OLE DB Operation class
</summary>
Public abstract class Opoledb
{
<summary>
Get database query string
</summary>
public static string connstr = configurationmanager.connectionstrings["qytangconnectionstring"]. ToString ();

<summary>
Gets the dataset based on a SQL
</summary>
<param name= "Con" > Database connection string </param>
<param name= "Cmdtext" >sql statement </param>
<param name= "parms" > Parameter list </param>
<returns>Dataset</returns>
public static DataSet GetDataSet (string con, String cmdtext, oledbparameter[] parms)
{
DataSet ds = new DataSet ();
OleDbCommand cmd = new OleDbCommand ();

OleDbConnection conn = new OleDbConnection (con);
OleDbDataAdapter da = new OleDbDataAdapter ();
PrepareCommand (CMD, conn, CommandType.Text, Cmdtext, parms);
Da. SelectCommand = cmd;
Da. Fill (DS);
Cmd. Parameters.clear ();
IF (Conn. state = = ConnectionState.Open)
Conn. Close ();
return DS;
}

<summary>
Read DataReader
</summary>
<param name= "Con" > Database connection string </param>
<param name= "Cmdtext" >sql statement </param>
<param name= "parms" > Parameter list </param>
<returns>DataReader</returns>
public static OleDbDataReader Getdataread (OleDbConnection conn, string Cmdtext, oledbparameter[] parms)
{
OleDbCommand cmd = new OleDbCommand ();
PrepareCommand (CMD, conn, CommandType.Text, Cmdtext, parms);
OleDbDataReader dr = cmd. ExecuteReader (commandbehavior.closeconnection);
Cmd. Parameters.clear ();
Return Dr;
}

<summary>
Insert, update, or delete operations
</summary>
<param name= "Con" > Database connection string </param>
<param name= "Cmdtext" >sql statement </param>
<param name= "parms" > Parameter list </param>
<returns> number of rows accepted for adjustment </returns>
public static int Query (string con, String cmdtext, oledbparameter[] parms)
{
OleDbCommand cmd = new OleDbCommand ();
OleDbConnection conn = new OleDbConnection (con);
PrepareCommand (CMD, conn, CommandType.Text, Cmdtext, parms);
int val = cmd. ExecuteNonQuery ();
Cmd. Parameters.clear ();
IF (Conn. state = = ConnectionState.Open)
Conn. Close ();
return Val;
}

Prepare for data queries
static void PrepareCommand (OleDbCommand cmd, OleDbConnection conn, CommandType Cmdtype, String cmdtext, oledbparameter[] Parms
{
IF (Conn. State = ConnectionState.Open)
Conn. Open ();
Cmd. Connection = conn;
Cmd.commandtext = Cmdtext;
Cmd.commandtype = Cmdtype;
if (parms! = null)
{
for (int i = 0; i < parms. Length; i++)
Cmd. Parameters.Add (Parms[i]);
}
}
}
}

Connection strings for Access databases and database operations classes

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.