Simple accsee database Encapsulation

Source: Internet
Author: User
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. UI;
Using system. Data. oledb;

Namespace solog. acesstask
...{
/** // <Summary>
/// Auxiliary class of the data operation layer
/// Provides data sources and SQL operations
/// </Summary>

Public class getdata: system. Web. UI. Page
...{
Private oledbconnection _ myconn;
Private Static string dbpath = httpcontext. Current. server. mappath (configurationmanager. deleetpipeline ["dbpath"]);

Public getdata ()
...{
String connectstring = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + dbpath;
_ Myconn = new oledbconnection (connectstring );
 
}

Public oledbconnection myconn ()
...{
Try
...{
_ Myconn. open ();
}
Catch (system. Data. oledb. oledbexception E)
...{
Throw new exception (E. Message );
}
Return _ myconn;

}
Public void closeconn ()
...{
_ Myconn. Close ();
}

Public dataset getdataset (string SQL)
...{
// Obtain Dataset
Dataset DS = new dataset ();
Try
...{
Oledbdataadapter DAP = new oledbdataadapter (SQL, this. myconn ());
Dap. Fill (DS );
Return Ds;
}
Catch (system. Data. oledb. oledbexception E)
...{
Throw new exception (E. Message );
}
Finally
...{
Closeconn ();
}
}

/** // <Summary>
/// Obtain oledatareader
/// </Summary>
/// <Param name = "sqlstring"> SQL statement </param>
/// <Returns> oledatareader </returns>
Public oledbdatareader getreader (string sqlstring)
...{
// Obtain idatareader
Oledbdatareader datareader = NULL;
Oledbcommand dbcmd = new oledbcommand (sqlstring, this. myconn ());

Try
...{
Datareader = dbcmd. executereader (commandbehavior. closeconnection );
Return datareader;
}
Catch (system. Data. oledb. oledbexception E)
...{
Throw new exception (E. Message );
}

}
Public void excnonquery (string sqlstring)
...{
// Execute a commadnd with no return value
Oledbcommand exc = new oledbcommand (sqlstring, this. myconn ());
Exc. executenonquery ();
Exc. Connection. Close ();
}
Public int excscalar (string sqlstring)
...{
// The execution returns a single int value.
Oledbcommand exc = new oledbcommand (sqlstring, this. myconn ());
Int result = convert. toint32 (EXC. executescalar ());
Exc. Connection. Close ();
Return result;

}

}
}

 

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.