Using system;
Using system. configuration;
Using system. collections;
Using system. Data;
Using system. Data. oledb;
Namespace mydata
{
Public static class class_oledb_conn
{
// Public static string connstr = @ "provider = Microsoft. Jet. oledb.4.0; Data Source = database/*. mdb ;";
Public static string connstr = mydata. properties. settings. Default. my_soft_oledbconn;
// Open the database link
Public static oledbconnection open_conn (string connstr)
{
Oledbconnection conn = new oledbconnection (connstr );
Conn. open ();
Return conn;
}
// Close the database link
Public static void close_conn (oledbconnection conn)
{
If (Conn! = NULL)
{
Conn. Close ();
Conn. Dispose ();
}
GC. Collect ();
}
// Run the oledb statement
Public static int run_ SQL (string SQL, string connstr)
{
Oledbconnection conn = open_conn (connstr );
Oledbcommand cmd = create_cmd (SQL, Conn );
Try
{
Int result_count = cmd. executenonquery ();
Close_conn (conn );
Return result_count;
}
Catch
{
Close_conn (conn );
Return 0;
}
}
// Generate command object
Public static oledbcommand create_cmd (string SQL, oledbconnection conn)
{
Oledbcommand cmd = new oledbcommand (SQL, Conn );
Return cmd;
}
// Run the oledb statement to return the datatable
Public static datatable get_datatable (string SQL, string connstr, string table_name)
{
Oledbdataadapter da = get_adapter (SQL, connstr );
Datatable dt = new datatable (table_name );
Da. Fill (DT );
Return DT;
}
// Run the oledb statement to return the oledbdatareader object
Public static oledbdatareader get_reader (string SQL, string connstr)
{
Oledbconnection conn = open_conn (connstr );
Oledbcommand cmd = create_cmd (SQL, Conn );
Oledbdatareader Dr;
Try
{
Dr = cmd. executereader (commandbehavior. Default );
}
Catch
{
Throw new exception (SQL );
}
Close_conn (conn );
Return Dr;
}
// Run the oledb statement to return the oledbdataadapter object
Public static oledbdataadapter get_adapter (string SQL, string connstr)
{
Oledbconnection conn = open_conn (connstr );
Oledbdataadapter da = new oledbdataadapter (SQL, Conn );
Return da;
}
// Run the oledb statement and return the DataSet object
Public static dataset get_dataset (string SQL, string connstr, dataset DS)
{
Oledbdataadapter da = get_adapter (SQL, connstr );
Try
{
Da. Fill (DS );
}
Catch (exception ERR)
{
Throw err;
}
Return Ds;
}
// Run the oledb statement and return the DataSet object
Public static dataset get_dataset (string SQL, string connstr, dataset ds, string tablename)
{
Oledbdataadapter da = get_adapter (SQL, connstr );
Try
{
Da. Fill (DS, tablename );
}
Catch (exception ex)
{
Throw ex;
}
Return Ds;
}
// Run the oledb statement, return the DataSet object, and paging the data
Public static dataset get_dataset (string SQL, string connstr, dataset ds, int startindex, int pagesize, string tablename)
{
Oledbconnection conn = open_conn (connstr );
Oledbdataadapter da = get_adapter (SQL, connstr );
Try
{
Da. Fill (DS, startindex, pagesize, tablename );
}
Catch (exception ex)
{
Throw ex;
}
Close_conn (conn );
Return Ds;
}
// Return the first column of the first row of the oledb statement execution result
Public static string get_row1_col1_value (string SQL, string connstr)
{
Oledbconnection conn = open_conn (connstr );
String result;
Oledbdatareader Dr;
Try
{
Dr = create_cmd (SQL, Conn). executereader ();
If (dr. Read ())
{
Result = Dr [0]. tostring ();
Dr. Close ();
}
Else
{
Result = "";
Dr. Close ();
}
}
Catch
{
Throw new exception (SQL );
}
Close_conn (conn );
Return result;
}
}
}
------------------------------------------------------------------
Database link string in APP. config
------------------------------------------------------------------
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
</Configsections>
<Connectionstrings>
<Add name = "mydata. properties. settings. my_soft_oledbconn" connectionstring = "provider = Microsoft. Jet. oledb.4.0; Data Source = database path \ *. mdb"
Providername = "system. Data. oledb"/>
</Connectionstrings>
</Configuration>