NET事务 隐式事务
如需运行分布式事务,需启动 T-SQL : CreateTableTranTable ( IdINTIDENTITY(1,1)PRIMARYKEY, PriorityTINYINT-- 最大值 255 ) C# : stringsql1="INSERTINTOTranTableVALUES(1)";// 正常插入 stringsql2="INSERTINTOTranTableVALUES(256)";// 插入异常 ADO
如需运行分布式事务,香港空间,美国服务器,需启动
T-SQL:
Create Table TranTable
(
Id INT IDENTITY(1,1) PRIMARY KEY,
Priority TINYINT--最大值255
)
C#:
string sql1 = "INSERT INTO TranTable VALUES(1)";//正常插入
string sql2 = "INSERT INTO TranTable VALUES(256)";//插入异常
ADO.NET事务
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MySqlServer"].ConnectionString))
{
conn.Open();
using (SqlTransaction tran = conn.BeginTransaction())
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.Transaction = tran;
try
{
cmd.CommandText = sql1;
cmd.ExecuteNonQuery();
cmd.CommandText = sql2;
cmd.ExecuteNonQuery();
tran.Commit();
Response.Write("Ok");
}
catch (SqlException ex)
{
tran.Rollback();
Response.Write("Error:" + ex.Message);
}
}
}
conn.Close();
}
ADO.NET
分布式隐式事务(TransactionScope)
隐式事务不具有
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MySqlServer"].ConnectionString))
{
using (TransactionScope ts = new TransactionScope())
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
try
{
cmd.CommandText = "insert into TranTable(Priority) values(1)";
cmd.ExecuteNonQuery();
cmd.CommandText = "insert into TranTable(Priority) values(256)";
cmd.ExecuteNonQuery();
ts.Complete();
Response.Write("Ok");
}
catch (SqlException ex)
{
Response.Write("Error:" + ex.Message);
}
}
}
conn.Close();
}
TransactionScope没有和数据库直接关联,那是怎么实现用事务的方式执行语句的呢?
如果我们在连接字符串里面加上
分布式显示事务(CommittableTransaction)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Lockwaittimeoutexceeded;tryrestartingtransaction - How to solve the MySQL error: transaction wait timeout. When using the MySQL database, you may sometimes encounter a common error: Lockwaittimeoutexceeded;tryrestartingtransaction. This error indicates that the transaction wait timeout. This error usually occurs when

MySQL transaction processing: the difference between automatic submission and manual submission. In the MySQL database, a transaction is a set of SQL statements. Either all executions are successful or all executions fail, ensuring the consistency and integrity of the data. In MySQL, transactions can be divided into automatic submission and manual submission. The difference lies in the timing of transaction submission and the scope of control over the transaction. The following will introduce the difference between automatic submission and manual submission in detail, and give specific code examples to illustrate. 1. Automatically submit in MySQL, if it is not displayed

The PHP Data Objects (PDO) extension provides efficient and object-oriented interaction with database servers. Its advanced query and update capabilities enable developers to perform complex database operations, improving performance and code maintainability. This article will delve into the advanced query and update functions of PDO and guide you to master its powerful functions. Advanced queries: Using placeholders and bound parameters Placeholders and bound parameters are important tools for improving query performance and security. Placeholders use question marks (?) to represent replaceable parameters in the query, while bind parameters allow you to specify the data type and value of each parameter. By using these methods, you can avoid SQL injection attacks and improve performance because the database engine can optimize queries ahead of time. //Use placeholder $stmt=$

1. Introduction to PDO PDO is an extension library of PHP, which provides an object-oriented way to operate the database. PDO supports a variety of databases, including Mysql, postgresql, oracle, SQLServer, etc. PDO enables developers to use a unified API to operate different databases, which allows developers to easily switch between different databases. 2. PDO connects to the database. To use PDO to connect to the database, you first need to create a PDO object. The constructor of the PDO object receives three parameters: database type, host name, database username and password. For example, the following code creates an object that connects to a mysql database: $dsn="mysq

Analysis of solutions to transaction management problems encountered in MongoDB technology development As modern applications become more and more complex and large, the transaction processing requirements for data are also getting higher and higher. As a popular NoSQL database, MongoDB has excellent performance and scalability in data management. However, MongoDB is relatively weak in data consistency and transaction management, posing challenges to developers. In this article, we will explore the transaction management issues encountered in MongoDB development and propose some solutions.

What is EJB? EJB is a Java Platform, Enterprise Edition (JavaEE) specification that defines a set of components for building server-side enterprise-class Java applications. EJB components encapsulate business logic and provide a set of services for handling transactions, concurrency, security, and other enterprise-level concerns. EJB Architecture EJB architecture includes the following major components: Enterprise Bean: This is the basic building block of EJB components, which encapsulates business logic and related data. EnterpriseBeans can be stateless (also called session beans) or stateful (also called entity beans). Session context: The session context provides information about the current client interaction, such as session ID and client

The principle and application scenarios of MySQL transactions In the database system, a transaction is a set of SQL operations. These operations are either all executed successfully or all fail and are rolled back. As a commonly used relational database management system, MySQL supports transaction characteristics and can ensure that the data in the database is consistent, isolated, durable and atomic. This article will start with the basic principles of MySQL transactions, introduce its application scenarios, and provide specific code examples for readers' reference. The principle of MySQL transactions: My

What are the transaction processing techniques for learning MySQL? Introduction: Transaction is a very important concept in database management system. It provides a mechanism to ensure data integrity and consistency. In MySQL, a transaction is the execution unit of a set of SQL statements, which can ensure that all SQL statements in this set of statements are either successfully executed or rolled back if all execution fails. This article will introduce the transaction processing skills of learning MySQL and give corresponding code examples. Start a transaction: In MySQL, you can use BEGIN and STARTTRAN
