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