AccessHelper, accesshelper. cs

Source: Internet
Author: User
Tags asp net

AccessHelper, accesshelper. cs

Code:

Using System; using System. data; using System. configuration; using System. data. oleDb; using ahwildlife. utils; // <summary> /// AccessHelper abstract description /// </summary> public class AccessHelper {# region variable protected static OleDbConnection conn = new OleDbConnection (); protected static OleDbCommand comm = new OleDbCommand (); protected static string connectionString = @ "Provider = Microsoft. jet. OLEDB.4.0; Data Source = Ahwildlife. mdb; Persist Security Info = False; Jet OLEDB: Database Password = sa ;"; # endregion # region constructor // <summary> // constructor // </summary> public AccessHelper () {}# endregion # region open the database // <summary> // open the database // </summary> private static void openConnection () {if (conn. state = ConnectionState. closed) {conn. connectionString = @ "Provider = Microsoft. jet. OLEDB.4.0; Data Source = ahwildlife. mdb; P Ersist Security Info = False; Jet OLEDB: Database Password = sa; "; comm. connection = conn; try {conn. open ();} catch (Exception ex) {throw new Exception (ex. message) ;}}# endregion # region closes the database /// <summary> // closes the database /// </summary> private static void closeConnection () {if (conn. state = ConnectionState. open) {conn. close (); conn. dispose (); comm. dispose () ;}# endregion # region executes the SQL statement // <summ Ary> // execute the SQL statement // </summary> public static void ExecuteSql (string sqlstr) {try {openConnection (); comm. commandType = CommandType. text; comm. commandText = sqlstr; comm. executeNonQuery ();} catch (Exception ex) {throw new Exception (ex. message) ;}finally {closeConnection () ;}# endregion # region returns the OleDbDataReader object of the specified SQL statement. Close this object when using it. /// <Summary> /// return the OleDbDataReader object of the specified SQL statement. Close this object when using it. /// </Summary> public static OleDbDataReader DataReader (string sqlstr) {OleDbDataReader dr = null; try {openConnection (); comm. commandText = sqlstr; comm. commandType = CommandType. text; dr = comm. executeReader (CommandBehavior. closeConnection);} catch {try {dr. close (); closeConnection () ;}catch {}} return dr ;}# endregion # region returns the OleDbDataReader object of the specified SQL statement, when using the service, Please disable it. /// <summary> /// the specified sq is returned. L OleDbDataReader object of the statement. When using the statement, close /// </summary> public static void DataReader (string sqlstr, ref OleDbDataReader dr) {try {openConnection (); comm. commandText = sqlstr; comm. commandType = CommandType. text; dr = comm. executeReader (CommandBehavior. closeConnection);} catch {try {if (dr! = Null &&! Dr. isClosed) dr. close () ;}catch {}finally {closeConnection ();}}} # endregion # region returns the DataSet of the specified SQL statement // <summary> // returns the DataSet of the specified SQL statement // </summary> /// <param name =" sqlstr "> </param> // <returns> </returns> public static DataSet (string sqlstr) {DataSet ds = new DataSet (); OleDbDataAdapter da = new OleDbDataAdapter (); try {openConnection (); comm. commandType = CommandType. text; c Omm. commandText = sqlstr; da. selectCommand = comm; da. fill (ds);} catch (Exception e) {throw new Exception (e. message) ;}finally {closeConnection () ;}return ds ;} # endregion # region returns the DataSet of the specified SQL statement // <summary> // returns the DataSet of the specified SQL statement // </summary> /// <param name =" sqlstr "> </param> // <param name =" ds "> </param> public static void DataSet (string sqlstr, ref DataSet ds) {OleDbDataAdapter da = New OleDbDataAdapter (); try {openConnection (); comm. commandType = CommandType. text; comm. commandText = sqlstr; da. selectCommand = comm; da. fill (ds);} catch (Exception e) {throw new Exception (e. message) ;}finally {closeConnection ();}} # endregion # region returns the able of the specified SQL statement // <summary> // returns the DataTable of the specified SQL statement // </summary> /// <param name =" sqlstr "> </param> // <returns> </returns> public st Atic DataTable (string sqlstr) {DataTable dt = Common. GetDataTableCache (sqlstr); // read cache if (dt! = Null) {return dt. copy () ;}else {dt = new able (); OleDbDataAdapter da = new OleDbDataAdapter (); try {using (OleDbConnection conn = new OleDbConnection () {conn. connectionString = connectionString; conn. open (); using (OleDbCommand comm = new OleDbCommand () {comm. connection = conn; comm. commandType = CommandType. text; comm. commandText = sqlstr; da. selectCommand = comm; da. fill (dt) ;}} catch (Exception e) {throw new Exception (e. message) ;}finally {closeConnection () ;} Common. insertDataTableCache (sqlstr, dt); // Add cache return dt. copy ();}} # endregion # region returns the able of the specified SQL statement // <summary> // returns the DataTable of the specified SQL statement // </summary> public static void DataTable (string sqlstr, ref DataTable dt) {OleDbDataAdapter da = new OleDbDataAdapter (); try {openConnection (); comm. commandType = CommandType. text; comm. commandText = sqlstr; da. selectCommand = comm; da. fill (dt);} catch (Exception e) {throw new Exception (e. message) ;}finally {closeConnection ();}} # endregion # region returns the DataView of the specified SQL statement // <summary> // returns the DataView of the specified SQL statement // </summary> /// <param name =" sqlstr "> </param> // <returns> </returns> public static DataView (string sqlstr) {OleDbDataAdapter da = new OleDbDataAdapter (); DataView dv = new DataView (); DataSet ds = new DataSet (); try {openConnection (); comm. commandType = CommandType. text; comm. commandText = sqlstr; da. selectCommand = comm; da. fill (ds); dv = ds. tables [0]. defaultView;} catch (Exception e) {throw new Exception (e. message) ;}finally {closeConnection () ;}return dv ;}# endregion}View Code

 


Who gives a C # AccessHelper class?
Asp net c #

SqlParameter [] paramList1 = // define a code pool
{
Sqlhelper. CreateInParam ("@ bigtype", SqlDbType. VarChar, 50, Request. QueryString ["pindao"]),
Sqlhelper. CreateInParam ("@ typeid", SqlDbType. VarChar, 18, dpdtype. SelectedItem. Text ),
Sqlhelper. CreateInParam ("@ title", SqlDbType. VarChar, 50, txttitle. Text ),
Sqlhelper. CreateInParam ("@ addadmin", SqlDbType. VarChar, 50, txtadmin. Text ),
Sqlhelper. CreateInParam ("@ author", SqlDbType. VarChar, 50, txtauthor. Text ),
Sqlhelper. CreateInParam ("@ abstract", SqlDbType. VarChar, 4000, txtjian. Text ),
Sqlhelper. CreateInParam ("@ body", SqlDbType. NText, 1073741823, FCKbody. Value ),
Sqlhelper. CreateInParam ("@ commentEnabled", SqlDbType. Int, 4, I ),
Sqlhelper. CreateInParam ("@ shouyetupian", SqlDbType. VarChar, 100, tupian ),
Sqlhelper. CreateInParam ("@ wzid", SqlDbType. Int, 4, Request. QueryString ["wzid"])};

Sqlhelper. RunProc ("EditArticle", paramList1 );

EditArticle is a stored procedure

Related Article

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.