提交事务并在c#中开始事务? [英] commit transaction and begin transaction in c#?

查看:223
本文介绍了提交事务并在c#中开始事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我只想知道使用c#语言在Asp.net中提交事务和开始事务的概念,我不知道所请求的概念,



i在c#中有一定的代码,由此我不知道我在哪里声明提交事务并开始交易。请绘制下面给出的代码。



我的代码是:



  public   void  dial( string  traid,  string  trakey, string  account, string  ttref )
{
尝试
{
string [ ] result = new string [ 4 ] ;
SqlConnection sqlconn_cms = new SqlConnection(sqlconn_cmsstr);

{
if (sqlconn_cms.State == ConnectionState.Closed)
{

sqlconn_cms.Open();
}
}
SqlCommand sqlCmd = new SqlCommand( < span class =code-string> ccTranscationchecktest,sqlconn_cms);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue( @ traid,traid);
sqlCmd.Parameters.AddWithValue( @ trakey,trakey);
sqlCmd.Parameters.AddWithValue( @ account,account);
sqlCmd.Parameters.AddWithValue( @ transref,ttref);
sqlCmd.ExecuteNonQuery();
sqlconn_cms.Close();
}
catch (例外情况)
{
}
}



如果你为我提供解决方案对我来说非常有用。所以请给出一个解决方案。



提前感谢

解决方案

交易由数据库处理引擎,因此 BEGIN TRANSACTION COMMIT TRANSACTION 应该是存储过程的一部分。


查看以下链接,了解交易情况



在ADO.NET中使用事务 [ ^ ]



希望这可以帮助你...


< blockquote>因为你的代码只执行一次操作 - 它只执行一次存储过程 - 很难真正告诉你事务发生了什么。



如果相反,我们做两个相互依赖的表更新应该是清楚的呃。

我们有两张桌子:

表1:

 ID Int 
Data NVARCHAR( 50)



表2:

 ID INT,Identity(由SQL分配因为我们不关心什么它只是它是唯一的)
Tab1ID INT,Tab1中ID的外键
数据NVARCHAR(50)



表2中的数据有一个外键:Table1中记录的ID。

将其视为发票(表1)和客户购买的单个项目(多个Table2条目)。

如果表1中没有该ID的记录,那么Tabel2数据无效。因此,如果第一个查询失败,我们不想尝试写第二个表数据。这很简单:

 私人 无效 TransDemo ( int  ID, string  dataForTab1, string  dataForTab2)
{
使用(SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
尝试
{
使用(SqlCommand cmd = new SqlCommand( INSERT INTO Table1(Id,Data)VALUES(@ ID,@ DT,con))
{
cmd.Parameters.AddWithValue( @ ID,ID);
cmd.Parameters.AddWithValue( @ DT,dataForTab1);
cmd.ExecuteNonQuery();
}
使用(SqlCommand cmd = new SqlCommand( INSERT INTO Table2(Tab1Id,Data)VALUES( @ ID,@ DT,con))
{
cmd.Parameters.AddWithValue( @ ID,ID);
cmd.Parameters.AddWithValue( @ DT,dataForTab2);
cmd.ExecuteNonQuery();
}
}
catch (例外情况)
{
MessageBox.Show(ex.Message) ;
}
}
}

try ... catch 块捕获问题,并阻止第二个正在写的表。但是如果第一个插入工作正常,但第二个插入失败会发生什么?这也很糟糕 - 因为第一个表格数据已被写入,但它没有连接到第二个表格。

你最终得到一张没有物品的发票 - 而你的公司就亏钱了!



这是交易进入的地方:在进行任何数据库更改之前启动事务,如果出现问题则撤消更改(称为回滚) )将它们扔掉,这样你的数据库就保持一致。如果一切正常,那么您批准所有更改(称为提交),然后才改变实际数据库。

  private   void  TransDemo( int  ID, string  dataForTab1, string  dataForTab2)
{
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
SqlTransaction trans = con.BeginTransaction();
尝试
{
使用(SqlCommand cmd = new SqlCommand( INSERT INTO Table1(Id,Data)VALUES(@ ID,@ DT,con))
{
cmd.Parameters.AddWithValue( @ ID,ID);
cmd.Parameters.AddWithValue( @ DT,dataForTab1);
cmd.ExecuteNonQuery();
}
使用(SqlCommand cmd = new SqlCommand( INSERT INTO Table2(Table1Id,Data)VALUES( @ ID,@ DT,con))
{
cmd.Parameters.AddWithValue( @ ID,ID);
cmd.Parameters.AddWithValue( @ DT,dataForTab2);
cmd.ExecuteNonQuery();
}
trans.Commit();
}
catch (例外情况)
{
trans.Rollback();
MessageBox.Show(ex.Message);
}
}
}

如果您遇到任何更新问题,Rollback会抛弃所有更改,因此问题出在哪里并不重要 - 它可以在第一个或第二个插入中,但数据库始终一致且正确。


Hi All,

I just want to know the concept of commit Transaction and Begin Transaction in Asp.net with c# language, i have no idea about the requested concept,

i have a certain code in c# given below by this i have no idea where i declare commit transaction and begin transaction. please draw a code given below.

my code is:

public void dial(string traid, string trakey, string account, string ttref)
{
    try
    {
        string[] result = new string[4];
        SqlConnection sqlconn_cms = new SqlConnection(sqlconn_cmsstr);

        {
            if (sqlconn_cms.State == ConnectionState.Closed)
            {

                sqlconn_cms.Open();
            }
        }
        SqlCommand sqlCmd = new SqlCommand("ccTranscationchecktest", sqlconn_cms);
        sqlCmd.CommandType = CommandType.StoredProcedure;
        sqlCmd.Parameters.AddWithValue("@traid", traid);
        sqlCmd.Parameters.AddWithValue("@trakey", trakey);
        sqlCmd.Parameters.AddWithValue("@account", account);
        sqlCmd.Parameters.AddWithValue("@transref", ttref);
        sqlCmd.ExecuteNonQuery();
        sqlconn_cms.Close();
    }
    catch (Exception ex)
    {
    }
}


if you drop an solution for me its very useful for me. So please give a solution for that.

thanks in advance

解决方案

Transactions are handled by the database engine, hence BEGIN TRANSACTION and COMMIT TRANSACTION should be part of the stored procedure.


Check the below link which gives fair idea about the transactions

Using Transactions in ADO.NET[^]

Hope this helps you ...


Because your code only performs a single operation - it executes a stored procedure once and once only - it's difficult to really show you what happens with a Transaction.

If instead we do two table updates that depend on each other it should be clearer.
We have two tables:
Table1:

ID     Int
Data   NVARCHAR(50)


Table2:

ID     INT, Identity (assigned by SQL because we don't care what it is, just that it is unique)
Tab1ID INT, Foreign key to ID in Tab1
Data   NVARCHAR(50)


The data in Table2 has a foreign key: the ID of the record in Table1.
Think of it as an Invoice (Table1) and the individual items the customer bought (multiple Table2 entries).
If there is no record with that ID in table 1, then the Tabel2 data is invalid. So if the first query fails, we don't want to try and write the second table data. That's easy:

private void TransDemo(int ID, string dataForTab1, string dataForTab2)
    {
    using (SqlConnection con = new SqlConnection(strConnect))
        {
        con.Open();
        try
            {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO Table1 (Id, Data) VALUES (@ID, @DT", con))
                {
                cmd.Parameters.AddWithValue("@ID", ID);
                cmd.Parameters.AddWithValue("@DT", dataForTab1);
                cmd.ExecuteNonQuery();
                }
            using (SqlCommand cmd = new SqlCommand("INSERT INTO Table2 (Tab1Id, Data) VALUES (@ID, @DT", con))
                {
                cmd.Parameters.AddWithValue("@ID", ID);
                cmd.Parameters.AddWithValue("@DT", dataForTab2);
                cmd.ExecuteNonQuery();
                }
            }
        catch (Exception ex)
            {
            MessageBox.Show(ex.Message);
            }
        }
    }

The try...catch block catches the problem, and prevents the second table being written. But what happens if the first insert works, but the second fails? That's also bad - because the first table data has been written, but it isn't "connected" to the second table.
You end up with an invoice for no items - and your company loses money!

This is where a Transaction comes in: You start a transaction before you do any database changes, and if there is a problem you undo the changes (called a "rollback") and throw them all away so your database remains consistent. If it all works, then you approve all the changes (called a "commit") and then and only then is the actual database altered.

private void TransDemo(int ID, string dataForTab1, string dataForTab2)
    {
    using (SqlConnection con = new SqlConnection(strConnect))
        {
        con.Open();
        SqlTransaction trans = con.BeginTransaction();
        try
            {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO Table1 (Id, Data) VALUES (@ID, @DT", con))
                {
                cmd.Parameters.AddWithValue("@ID", ID);
                cmd.Parameters.AddWithValue("@DT", dataForTab1);
                cmd.ExecuteNonQuery();
                }
            using (SqlCommand cmd = new SqlCommand("INSERT INTO Table2 (Table1Id, Data) VALUES (@ID, @DT", con))
                {
                cmd.Parameters.AddWithValue("@ID", ID);
                cmd.Parameters.AddWithValue("@DT", dataForTab2);
                cmd.ExecuteNonQuery();
                }
            trans.Commit();
            }
        catch (Exception ex)
            {
            trans.Rollback();
            MessageBox.Show(ex.Message);
            }
        }
    }

If you get a problem with either update, the Rollback throws away all the changes, so it doesn't matter where the problem was - it can be in the first or second insert but the database is always consistent and right.


这篇关于提交事务并在c#中开始事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆