C # Database Access static classes support multiple SQL statement transaction operations

Source: Internet
Author: User

Using System; <br/> using System. data; <br/> using System. data. sqlClient; <br/> using System. collections; </p> <p> namespace ReCard <br/>{< br/> public static class DB <br/>{</p> <p> private static SqlConnection connection = null; <br/> private static SqlCommand command = null; <br/> private static SqlDataAdapter adapter = null; <br/> private static string errorString; <br/> public static string ErrorStr Ing <br/>{< br/> get {return errorString ;} <br/>}</p> <p> // <summary> <br/> // initialization <br/> /// </summary> <br/> public static void Init (string ConnectionString) <br/>{< br/> connection = new SqlConnection (ConnectionString); <br/> command = connection. createCommand (); <br/> adapter = new SqlDataAdapter (); <br/> adapter. selectCommand = command; <br/>}</p> <p> // <summary> <br/> // open the connection. <br/> /// </Summary> <br/> /// <returns> </returns> <br/> public static bool Connect () <br/>{< br/> try <br/>{< br/> if (connection. state = ConnectionState. closed) <br/>{< br/> connection. open (); <br/>}< br/> return true; <br/>}< br/> catch (Exception) <br/>{< br/> return false; <br/>}</p> <p> // <summary> <br/> // close the connection <br/> // </summary> <br/> // <returns> </returns> <br/> public stat Ic bool CloseConnect () <br/>{< br/> try <br/>{< br/> if (connection. state = ConnectionState. open) <br/>{< br/> connection. close (); <br/>}< br/> return true; <br/>}< br/> catch (Exception) <br/>{< br/> return false; <br/>}</p> <p> // <summary> <br/> // retrieve data from the database based on the query statement <br/> /// </summary> <br/> /// <param name = "strSelect"> query statement </param> <br/> /// <returns> has the data returns the DataTable object, otherwise, null is returned. </r Eturns> <br/> public static DataTable DTSelect (string SelectString) <br/>{< br/> errorString = ""; <br/> try <br/> {<br/> if (connection. state = ConnectionState. closed) <br/>{< br/> connection. open (); <br/>}< br/> adapter. selectCommand. commandType = CommandType. text; <br/> adapter. selectCommand. commandText = SelectString; <br/> DataTable myDT = new DataTable (); <br/> adapter. fill (myDT); <br/> Return myDT; <br/>}< br/> catch (Exception ex) <br/>{< br/> errorString = "Data Query failed:" + ex. message; <br/> return null; <br/>}</p> <p> // <summary> <br/> // update the database based on SQL statements <br/>/ /// </summary> <br/> /// <param name = "UpdataString"> Update statement </param> <br/> /// <returns> returns true </returns> <br/> public static bool dbUpdate (string UpdataString) <br/>{< br/> errorString = ""; <br/> try <br/> {<br /> If (connection. state = ConnectionState. closed) <br/>{< br/> connection. open (); <br/>}< br/> command. commandType = CommandType. text; <br/> command. commandText = UpdataString; <br/> int intCount = command. executeNonQuery (); <br/> return intCount> 0; <br/>}< br/> catch (Exception ex) <br/>{< br/> errorString = "failed to update database:" + ex. message; <br/> return false; <br/>}</p> <p> // <summary> <Br/> // execute multiple SQL statements to implement database transactions. <Br/> /// </summary> <br/> /// <param name = "SQLStringList"> Multiple SQL statements </param> <br/> public static bool ExecuteSqlTran (ArrayList SQLStringList) <br/>{</p> <p> SqlTransaction tx = connection. beginTransaction (); <br/> command. transaction = tx; <br/> try <br/> {<br/> for (int n = 0; n <SQLStringList. count; n ++) <br/>{< br/> string strsql = SQLStringList [n]. toString (); <br/> if (strsql. trim (). length> 1) <br/>{< br/> command. commandText = strsql; <br/> command. executeNonQuery (); <br/>}< br/> tx. commit (); <br/> return true; <br/>}< br/> catch (Exception Etranca) <br/>{< br/> tx. rollback (); <br/> errorString = "database operation failed:" + Etranca. message; <br/> return false; <br/>}</p> <p >}< br/>

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.