Usage of fee-based system affairs for individual restructured data centers

Source: Internet
Author: User

Although it is not the first time to create a data center charging system, I also know the requirements of the data center charging system. However, if I want to create a data center charging system that I am satisfied with, I will have to worry about it. I spent a long time completing a transaction.

Transaction: combines multiple operations related to each other into a logical unit to ensure that these operations are either executed in full or not executed at all.

When using transactions in ADO. net, sqlconnection and
The oledbconnection object contains a begintransaction method, which can return sqltransaction or oledbtransaction. The transaction object has the commit and rollback methods to manage transactions in the application.

Transaction execution steps: (here, the SQL database prevails)

(1) Call the begintransaction method of the connection object and assign the return value to a variable of the sqltransaction type.

Example: dim mytrans as (new) sqltransaction

Dim conn as new sqlconnection

Mytrans = conn. begintransaction ()

 

(2) set the transaction attribute for all objects to be executed in the transaction to reference the transaction object.

Dim cmd as sqlcommand

Cmd. Transaction = mytrans

(3) execute necessary command objects.

(4) If the command is complete, the commit method of the transaction object is called. If any problem occurs, rollback will be called to roll to the initial condition.

The following figure shows how to use the DLL-Layer Code (student registration) of the data center billing system:


I have created two tables for student registration in my own database: stuinfo and
Cardinfo is the two tables, so I use a transaction to add student information to the two tables. (If you can add the tables, add them at the same time. If you cannot add the tables, do not add them. This is what I meant to use transactions .)

The specific D-Layer Code is as follows:

Imports system. data. sqlclientpublic class sturegistdal: Implements idal. iregister 'registers public function register_idal (byval cardinfo as entity. cardinfoentity, byval stuinfo as entity. stuinfoentity) as Boolean implements idal. iregister. register_idal 'defines the string dim STR as string = "Data Source = 192.168.24.76; initial catalog = charge_system; uid = sa; pwd = 123456 "'instantiate conn and use the conn dim conn as new sqlclient to initialize conn dim. sqlconnection (STR) 'defines the transaction type variable dim mytrans as sqltransaction' instantiate sqlcommand class dim cmd as new sqlcommand 'register try' to Open Database Conn. open () 'calls the begintransaction method of sqltransaction to start a local transaction, and assigns the returned sqltransaction object to tstrans mytrans = Conn. begintransaction 'associates the conn object with Cmd. connection = conn' associate the mytrans object with Cmd. transaction = mytrans dim SQL as string = "insert into t_cardinfo (cardno, cash, registerdate, registertime, ischeck, userid)" & _ "values ('" & cardinfo. cardno & "','" & cardinfo. cash & "','" & date. today & "','" & timeofday & "','" & cardinfo. ischeck & "','" & cardinfo. userid & "')"' associates the CMD object with the tstrans object. commandtext = SQL cmd. executenonquery () 'adds the student table information to the student table SQL = "insert into t_stuinfo (stuno, cardno, stuname, sex, stuclass, grade, department) "& _" values ('"& stuinfo. stuno & "','" & stuinfo. cardno & "','" & stuinfo. stuname & "','" & stuinfo. sex & "','" & stuinfo. stuclass & "','" & stuinfo. grade & "','" & stuinfo. department & "')" cmd. commandtext = SQL cmd. executenonquery () mytrans. commit () return true catch ex as exception 'Leave the transaction object empty mytrans = noder' to allow the transaction to roll back mytrans. rollback () 'returns false to other layers. Based on the returned value, it determines whether the registration is successful (if false is not successful, of course). Return false end try end functionend class


In this way, both data tables can be added at the same time.




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.