illustrates the use of transactions
来源:互联网 发布:美工培训上海 编辑:程序博客网 时间:2024/05/12 06:50
using System;
using System.Data;
using System.Data.SqlClient;
public class Example23_4
{
public static void Main()
{
// formulate a string containing the details of the
// database connection
string connectionString =
"server=localhost;database=Northwind;uid=sa;pwd=sa";
// create a SqlConnection object to connect to the
// database, passing the connection string to the constructor
SqlConnection mySqlConnection =
new SqlConnection(connectionString);
// open the database connection using the
// Open() method of the SqlConnection object
mySqlConnection.Open();
// step 1: create a SqlTransaction object and start the transaction
// by calling the BeginTransaction() method of the SqlConnection
// object
SqlTransaction mySqlTransaction =
mySqlConnection.BeginTransaction();
// step 2: create a SqlCommand object to hold a SQL statement
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
// step 3: set the Transaction property for the SqlCommand object
mySqlCommand.Transaction = mySqlTransaction;
// step 4: formulate a string containing the first INSERT statement
string insertString =
"INSERT INTO Customers (" +
" CustomerID, CompanyName, ContactName, Address" +
") VALUES (" +
" 'T2COM', 'T2 Company', 'Jason Price', '1 Main Street'" +
")";
// step 5: set the CommandText property of the SqlCommand object to
// the INSERT string
mySqlCommand.CommandText = insertString;
// step 6: run the first INSERT statement
Console.WriteLine("Running first INSERT statement");
mySqlCommand.ExecuteNonQuery();
// step 7: formulate a second INSERT statement
insertString =
"INSERT INTO Orders (" +
" CustomerID" +
") VALUES (" +
" 'T2COM'" +
")";
// step 8: set the CommandText property of the SqlCommand object to
// the second INSERT string
mySqlCommand.CommandText = insertString;
// step 9: run the second INSERT statement
Console.WriteLine("Running second INSERT statement");
mySqlCommand.ExecuteNonQuery();
// step 10: commit the transaction using the Commit() method
// of the SqlTransaction object
Console.WriteLine("Committing transaction");
mySqlTransaction.Commit();
// close the database connection using the Close() method
// of the SqlConnection object
mySqlConnection.Close();
}
}
using System.Data;
using System.Data.SqlClient;
public class Example23_4
{
public static void Main()
{
// formulate a string containing the details of the
// database connection
string connectionString =
"server=localhost;database=Northwind;uid=sa;pwd=sa";
// create a SqlConnection object to connect to the
// database, passing the connection string to the constructor
SqlConnection mySqlConnection =
new SqlConnection(connectionString);
// open the database connection using the
// Open() method of the SqlConnection object
mySqlConnection.Open();
// step 1: create a SqlTransaction object and start the transaction
// by calling the BeginTransaction() method of the SqlConnection
// object
SqlTransaction mySqlTransaction =
mySqlConnection.BeginTransaction();
// step 2: create a SqlCommand object to hold a SQL statement
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
// step 3: set the Transaction property for the SqlCommand object
mySqlCommand.Transaction = mySqlTransaction;
// step 4: formulate a string containing the first INSERT statement
string insertString =
"INSERT INTO Customers (" +
" CustomerID, CompanyName, ContactName, Address" +
") VALUES (" +
" 'T2COM', 'T2 Company', 'Jason Price', '1 Main Street'" +
")";
// step 5: set the CommandText property of the SqlCommand object to
// the INSERT string
mySqlCommand.CommandText = insertString;
// step 6: run the first INSERT statement
Console.WriteLine("Running first INSERT statement");
mySqlCommand.ExecuteNonQuery();
// step 7: formulate a second INSERT statement
insertString =
"INSERT INTO Orders (" +
" CustomerID" +
") VALUES (" +
" 'T2COM'" +
")";
// step 8: set the CommandText property of the SqlCommand object to
// the second INSERT string
mySqlCommand.CommandText = insertString;
// step 9: run the second INSERT statement
Console.WriteLine("Running second INSERT statement");
mySqlCommand.ExecuteNonQuery();
// step 10: commit the transaction using the Commit() method
// of the SqlTransaction object
Console.WriteLine("Committing transaction");
mySqlTransaction.Commit();
// close the database connection using the Close() method
// of the SqlConnection object
mySqlConnection.Close();
}
}
- illustrates the use of transactions
- illustrates the use of a DataView object to filter and sort rows
- This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL, a.k.a. selection mode on GridView
- The use of cookies
- The use of Locale
- The use of MD_STOCK_REQUIREMENTS_LIST_API
- The use of NSBundle!
- the use of mmap
- The use of functions
- The use of functions
- 102.Examine the structure of the TRANSACTIONS table:
- 103.Examine the structure of the TRANSACTIONS table:
- The use method of Transation
- The False Use of SVN
- Simple Use of the Clipboard
- The daily use of sql2008
- The Beginning use of Polymorphism
- What is the use of "?
- Use A Data Reader
- 物化视图的效率
- illustrates how to call a SQL Server stored procedure
- Call a store procedure
- want使用方法
- illustrates the use of transactions
- Update Data Using Transactions
- 三国略史
- [转]华为公司员工待遇全面揭秘
- Tomcat5.5虚拟目录设定(转)
- Creating Data Tables and Populating Them
- 树结构数据装入delphi的TTreeview组件中
- 理解索引的选择性和密度
- Swt常用控件中文教程