C # Two methods of accessing a database and two methods of transactions

Source: Internet
Author: User

1 //2015/07/032 usingSystem;3 usingSystem.Collections.Generic;4 usingSystem.Linq;5 usingSystem.Text;6 usingSystem.Threading.Tasks;7 usingSystem.Data;8 usingSystem.Data.SqlClient;9 Ten namespaceado.netstart_1 One { A     class Program -     { -         Static voidMain (string[] args) the         { -             //special string, database connection string -             //First Kind -             stringConnectionString ="server=.; database=booksample;uid=ljk;pwd=123456;"; +             //The second Kind -             //Windows Authentication +             //when Windows authentication is used, Windows users are the users who run the program A             //string connectionString = "server=.; Integrated security=true;database=booksample "; at  -             //classes used to access the database -SqlConnection connection =NewSqlConnection (connectionString); -  -             Try -             { in                 //establish a connection to the database - connection. Open (); toConsole.WriteLine ("Connection already established"); +  -                 //SQL is a string the                 stringsql ="INSERT into students (Id,stuname,phone,address,city,state) VALUES (7, ' AAA ', ' 1318697125 ', ' zz ', ' BB ', ' CCC ')"; *                 //SqlCommand represents the Send Database execution SQL command $SqlCommand Command =NewSqlCommand (sql,connection);Panax Notoginseng                 //Send command to database execution -                 //SQL statements can be divided into: query statements and non-query statements the  +                 //The default work is under automatic transactions A                 //if work is under manual transaction the                 //BEGIN TRANSACTION +                 //Rollback/commit -  $                 //before executing SQL DML, start a transaction $SqlTransaction trans = connection. BeginTransaction ();//Transaction Object -                  -                 //set the transaction management to which the command belongs theCommand. Transaction =Trans; - Wuyi  the                 intEffectcount =command. ExecuteNonQuery (); -Console.WriteLine ("{0} rows affected", effectcount); Wu  -Console.Write ("the command has been executed and the transaction has not been committed, are you sure you want to commit? y/n"); About                 stringline =console.readline (); $                 if(line. Substring (0,1). ToUpper () = ="Y") -                 { - trans.commit (); -Console.WriteLine ("transaction has been submitted"); A                 } +                 Else the                 { - Trans. Rollback (); $Console.WriteLine ("transaction has been rolled back"); the                 } the             } the             Catch(SqlException exception) the             { - Console.WriteLine (Exception. Message); in             } the             finally the             {  About                 //or disconnect from the database the connection. Dispose (); the                  //connection. Close (); theConsole.WriteLine ("already disconnected"); +             } - Console.readkey (); the         }Bayi     } the}

C # Two methods of accessing a database and two methods of transactions

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.