DBHELP statements
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data;usingSystem.Data.SqlClient;namespaceAnalog ATM Transfer {Static classDbhelp { Public Static BOOLIdubysql (stringSQL) { BOOLFA =false; stringstr ="server=.; Integrated SECURITY=TRUE;DATABASE=BANKDB"; SqlConnection sqlconn=NewSqlConnection (str); SqlCommand Sqlcomm=NewSqlCommand (SQL, sqlconn); Try{sqlconn. Open (); //Open TransactionSqlcomm. Transaction =sqlconn. BeginTransaction (); FA= Sqlcomm. ExecuteNonQuery () >0?true:false; //Commit a transactionSqlcomm.Transaction.Commit (); } Catch(SqlException ex) {//rolling back a transactionSqlcomm. Transaction.rollback (); } finally{sqlconn. Close (); } returnFA; } Public StaticDataTable Gettablebysql (stringSQL) {DataTable Ta=NewDataTable (); stringstr ="server=.; Integrated SECURITY=TRUE;DATABASE=BANKDB"; SqlConnection sqlconn=NewSqlConnection (str); SqlCommand Sqlcomm=NewSqlCommand (SQL, sqlconn); SqlDataAdapter da=NewSqlDataAdapter (Sqlcomm); Da. Fill (TA); returnTa; } }}
SQL statements
--G. Transactions:--define the transfer transaction, and implement the transfer operation to test, note: During the transfer process is actually two transactions, one is "deposit", one is "withdraw", note the addition of transactions to the Transaction information tablebegin TranTran_bankDeclare @cou intSet @cou=0UpdateCardinfoSetBalance=Balance- the whereCardID='1010 3576 1234 5678'UpdateCardinfoSetBalance=Balance+ - whereCardID='1010 3576 1212 1134'Set @cou=@ @error+@couif(@cou<>0) begin rollback Tran--Roll back EndElse begin Commit Tran Insert intoTransinfo (Cardid,transtype, Transmoney,remark)Values('1010 3576 1212 1134','deposited', -,'John Doe Deposit') Insert intoTransinfo (Cardid,transtype, Transmoney,remark)Values('1010 3576 1234 5678','withdrawal', the,'Zhang San withdrawal') Print 'The transaction was successful to save the new data' EndGo
DBHELP and SQL statements with things to deal with