BeginTransaction在代码中的位置 [英] Place of BeginTransaction in code

查看:56
本文介绍了BeginTransaction在代码中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

考虑这段代码


SqlConnection oConn = new

SqlConnection(AccountsConnectionString);

SqlCommand cmdInsert = new SqlCommand(" UpdateQuery");

//位置1:

// SqlTransaction sqlTran = oConn.BeginTransaction();

试试

{

oConn.Open();

//位置2:

// SqlTransaction sqlTran = oConn.BeginTransaction();

cmdInsert.ExecuteNonQuery();

sqlTran.Commit();

} < br $>
catch(exception ex)

{

//位置3:

sqlTran.Rollback();

}

终于

{

oConn.Close();

}


我必须启动事务并提交或回滚,具体取决于命令执行成功的条件。问题是如果我在第1位写入开始交易的
代码,它就不会工作,因为连接是

仍然关闭。如果我在位置2写同样的东西,我就不能在位置3处回滚
回滚事务,因为sqlTran对象没有来自

referance。我该怎么办?我正在使用框架2.0。


谢谢和问候

Chakravarti Mukesh

Hi all,
Consider this code

SqlConnection oConn = new
SqlConnection(AccountsConnectionString);
SqlCommand cmdInsert = new SqlCommand("UpdateQuery");
//Location 1:
//SqlTransaction sqlTran = oConn.BeginTransaction();
try
{
oConn.Open();
//Location 2:
//SqlTransaction sqlTran = oConn.BeginTransaction();
cmdInsert.ExecuteNonQuery();
sqlTran.Commit();
}
catch (Exception ex)
{
//Location 3:
sqlTran.Rollback();
}
finally
{
oConn.Close();
}

I have to start a transaction and commit or rollback depending upon the
condition of success of command execution. The problem is if I write
code to Begin Transaction at location 1 it won''t work as connection is
still closed. If I write the same at location 2, I won''t be able to
rollback transaction at location 3 as sqlTran object is out of
referance. What should I do? I am using framework 2.0.

Thanks and Regards
Chakravarti Mukesh

推荐答案

10月10日上午8:09,Mukesh < cmukes ... @ gmail.comwrote:
On Oct 10, 8:09 am, "Mukesh" <cmukes...@gmail.comwrote:

SqlConnection oConn = new

SqlConnection(AccountsConnectionString);

SqlCommand cmdInsert = new SqlCommand(" UpdateQuery");

//位置1:

// SqlTransaction sqlTran = oConn.BeginTransaction();

试试

{

oConn.Open();

//位置2:

/ / SqlTransaction sqlTran = oConn.BeginTransaction();

cmdInsert.ExecuteNonQuery();

sqlTran.Commit();

}

catch(Exception ex)

{

//位置3:

sqlTran.Rollback();

}

终于

{

oConn.Close();

}
SqlConnection oConn = new
SqlConnection(AccountsConnectionString);
SqlCommand cmdInsert = new SqlCommand("UpdateQuery");
//Location 1:
//SqlTransaction sqlTran = oConn.BeginTransaction();
try
{
oConn.Open();
//Location 2:
//SqlTransaction sqlTran = oConn.BeginTransaction();
cmdInsert.ExecuteNonQuery();
sqlTran.Commit();
}
catch (Exception ex)
{
//Location 3:
sqlTran.Rollback();
}
finally
{
oConn.Close();
}



在Loc中声明事务变量第1步,但开始吧

位置2:

SqlConnection oConn = new

SqlConnection(AccountsConnectionString);

SqlCommand cmdInsert = new SqlCommand(" UpdateQuery");

//位置1:

SqlTransaction sqlTran = null;

尝试

{

oConn.Open();

//位置2:

sqlTran = oConn。 BeginTransaction();

cmdInsert.ExecuteNonQuery();

sqlTran.Commit();

}

catch (例外情况)

{

//位置3:

sqlTran.Rollback();

}

终于

{

oConn.Close();

}

Declare the transaction variable in Location 1, but Begin it in
location 2:

SqlConnection oConn = new
SqlConnection(AccountsConnectionString);
SqlCommand cmdInsert = new SqlCommand("UpdateQuery");
//Location 1:
SqlTransaction sqlTran = null;
try
{
oConn.Open();
//Location 2:
sqlTran = oConn.BeginTransaction();
cmdInsert.ExecuteNonQuery();
sqlTran.Commit();
}
catch (Exception ex)
{
//Location 3:
sqlTran.Rollback();
}
finally
{
oConn.Close();
}


在try / catch之外声明事务对象。只是不要把它设置为

任何东西。然后,在打开连接后,在try块内,将变量设置为新事务。


没有法律规定你必须声明变量并将其设置为

在同一行中。您可以在

方法的顶部声明所有内容,然后再使用它。事实上,这是我个人的偏好,

只是为了查看我方法中涉及的所有变量,所以我没有必要

在整个方法中跟踪它们。


" Mukesh" < cm ******* @ gmail.comwrote in message

news:11 ********************* @ k70g2000cwa .googlegro ups.com ...
Declare the transaction object outside the try/catch. Just don''t set it to
anything. Then, after you open the connection, inside the try block, set
the variable to the new transaction.

There is no law that says you have to declare the variable and set it to
something in the same line. You can declare everything at the top of your
method, and then use it later. In fact, that is my personal preference,
just to see all the variables involved in my method, so I don''t have to
track them down throughout the method.

"Mukesh" <cm*******@gmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...

大家好,

考虑这段代码


SqlConnection oConn = new

SqlConnection(AccountsConnectionString);

SqlCommand cmdInsert = new SqlCommand(" UpdateQuery");

//位置1:

// SqlTransaction sqlTran = oConn.BeginTransaction();

尝试

{

oConn.Open() ;

//位置2:

// SqlTransaction sqlTran = oConn.BeginTransaction();

cmdInsert.ExecuteNonQuery();

sqlTran.Commit();

}

catch(例外情况)

{

//位置3:

sqlTran.Rollback();

}

终于

{

oConn.Close();

}


我必须根据

条件启动事务并提交或回滚命令执行的成功。问题是如果我在第1位写入开始交易的
代码,它就不会工作,因为连接是

仍然关闭。如果我在位置2写同样的东西,我就不能在位置3处回滚
回滚事务,因为sqlTran对象没有来自

referance。我该怎么办?我正在使用框架2.0。


谢谢和问候

Chakravarti Mukesh
Hi all,
Consider this code

SqlConnection oConn = new
SqlConnection(AccountsConnectionString);
SqlCommand cmdInsert = new SqlCommand("UpdateQuery");
//Location 1:
//SqlTransaction sqlTran = oConn.BeginTransaction();
try
{
oConn.Open();
//Location 2:
//SqlTransaction sqlTran = oConn.BeginTransaction();
cmdInsert.ExecuteNonQuery();
sqlTran.Commit();
}
catch (Exception ex)
{
//Location 3:
sqlTran.Rollback();
}
finally
{
oConn.Close();
}

I have to start a transaction and commit or rollback depending upon the
condition of success of command execution. The problem is if I write
code to Begin Transaction at location 1 it won''t work as connection is
still closed. If I write the same at location 2, I won''t be able to
rollback transaction at location 3 as sqlTran object is out of
referance. What should I do? I am using framework 2.0.

Thanks and Regards
Chakravarti Mukesh




实际上我在发布之前尝试在位置1上声明它。但是当你写完b
时,我并没有把它设置成任何东西。因此,我收到错误

未找到对象引用...。但是当我把它设置为null时,Chris

建议,它工作正常。


谢谢和问候

Chakravarti Mukesh


Marina Levit [MVP]写道:
Hi,
In fact I tried declaring it on location 1 before posting. But as you
wrote, I was not setting it to anything. Thus I am getting the error
"Object reference not found...". But when I set it to null as Chris
suggested, it is working fine.

Thanks and Regards
Chakravarti Mukesh

Marina Levit [MVP] wrote:

在try / catch之外声明事务对象。只是不要把它设置为

任何东西。然后,在打开连接后,在try块内,将变量设置为新事务。


没有法律规定你必须声明变量并将其设置为

在同一行中。您可以在

方法的顶部声明所有内容,然后再使用它。事实上,这是我个人的偏好,

只是为了查看我方法中涉及的所有变量,所以我没有必要

在整个方法中跟踪它们。


" Mukesh" < cm ******* @ gmail.comwrote in message

news:11 ********************* @ k70g2000cwa .googlegro ups.com ...
Declare the transaction object outside the try/catch. Just don''t set it to
anything. Then, after you open the connection, inside the try block, set
the variable to the new transaction.

There is no law that says you have to declare the variable and set it to
something in the same line. You can declare everything at the top of your
method, and then use it later. In fact, that is my personal preference,
just to see all the variables involved in my method, so I don''t have to
track them down throughout the method.

"Mukesh" <cm*******@gmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...

大家好,

考虑这段代码


SqlConnection oConn = new

SqlConnection(AccountsConnectionString);

SqlCommand cmdInsert = new SqlCommand(" UpdateQuery");

//位置1:

// SqlTransaction sqlTran = oConn.BeginTransaction();

尝试

{

oConn.Open() ;

//位置2:

// SqlTransaction sqlTran = oConn.BeginTransaction();

cmdInsert.ExecuteNonQuery();

sqlTran.Commit();

}

catch(例外情况)

{

//位置3:

sqlTran.Rollback();

}

终于

{

oConn.Close();

}


我必须启动一个事务并提交或回滚,具体取决于在

命令执行成功的条件。问题是如果我在第1位写入开始交易的
代码,它就不会工作,因为连接是

仍然关闭。如果我在位置2写同样的东西,我就不能在位置3处回滚
回滚事务,因为sqlTran对象没有来自

referance。我该怎么办?我正在使用框架2.0。


谢谢和问候

Chakravarti Mukesh
Hi all,
Consider this code

SqlConnection oConn = new
SqlConnection(AccountsConnectionString);
SqlCommand cmdInsert = new SqlCommand("UpdateQuery");
//Location 1:
//SqlTransaction sqlTran = oConn.BeginTransaction();
try
{
oConn.Open();
//Location 2:
//SqlTransaction sqlTran = oConn.BeginTransaction();
cmdInsert.ExecuteNonQuery();
sqlTran.Commit();
}
catch (Exception ex)
{
//Location 3:
sqlTran.Rollback();
}
finally
{
oConn.Close();
}

I have to start a transaction and commit or rollback depending upon the
condition of success of command execution. The problem is if I write
code to Begin Transaction at location 1 it won''t work as connection is
still closed. If I write the same at location 2, I won''t be able to
rollback transaction at location 3 as sqlTran object is out of
referance. What should I do? I am using framework 2.0.

Thanks and Regards
Chakravarti Mukesh

这篇关于BeginTransaction在代码中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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