ADO. NET data operations, ADO. NET data operations

Source: Internet
Author: User

ADO. NET data operations, ADO. NET data operations

Public static class SQLHelper {static string connString = ConfigurationManager. connectionStrings ["connString"]. toString (); /// <summary> /// execute add, delete, modify, // </summary> /// <param name = "SQL"> </param> /// <returns> </returns> public static int Update (string SQL) {SqlConnection conn = new SqlConnection (connString); SqlCommand cmd = new SqlCommand (SQL, conn); try {conn. open (); return cmd. executeNonQuery ();} catch (Exception ex) {throw new Exception (ex. message);} finally {conn. close ();}} /// <summary> /// execute a single result query /// </summary> /// <param name = "SQL"> </param> /// <returns> </returns> public static object GetSingleResult (string SQL) {SqlConnection conn = new SqlConnection (connString); SqlCommand cmd = new SqlCommand (SQL, conn); try {conn. open (); return cmd. executeScalar ();} catch (Exception ex) {throw new Exception (ex. message);} finally {conn. close ();}} /// <summary> /// execute a result set query /// </summary> /// <param name = "SQL"> </param> // <returns> </returns> public static SqlDataReader GetReader (string SQL) {SqlConnection conn = new SqlConnection (connString); SqlCommand cmd = new SqlCommand (SQL, conn); try {conn. open (); return cmd. executeReader (CommandBehavior. closeConnection);} catch (Exception ex) {conn. close (); throw new Exception (ex. message);} finally {conn. close ();}} /// <summary> /// execute the query on the returned dataset /// </summary> /// <param name = "SQL"> </param> // <returns> </returns> public static SqlDataAdapter GetDataSet (string SQL) {SqlConnection conn = new SqlConnection (connString); SqlCommand cmd = new SqlCommand (SQL, conn); // create the adapter object SqlDataAdapter da = new SqlDataAdapter (cmd ); dataSet ds = new DataSet (); try {conn. open (); da. fill (ds); // use the data adapter to Fill the dataset return da;} catch (Exception ex) {throw new Exception (ex. message);} finally {conn. close ();}}}

Common Methods for database operations

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.