在sqltransaction中是否类似 [英] Is it similar in sqltransaction

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

问题描述

SqlConnection cn;
SqlTransaction st;
st=cn.BeginTransaction;




V/s

SqlConnection cn;
SqlTransaction st =新的SqlTransaction();


相似吗?

我尝试过的事情:

我曾在Google上搜索过,但没有理解并感到困惑.




V/s

SqlConnection cn;
SqlTransaction st=new SqlTransaction();


Is it similar?

What I have tried:

I had searched on google but not understand and confused.

private void btbsend_Click(object sender, EventArgs e)
        {
                MySqlCommand genvoucher,df;
                MySqlTransaction genvouchrt=new MySqlTransaction();
                MySqlTransaction dft = new MySqlTransaction();
                try
                {
                    cn.Open();

                    string fvno;
                    dateTimePicker1.Value = DateTime.Now;
                    dateTimePicker2.Value = DateTime.Now;
                    var tdat = dateTimePicker2.Value;
                    var ttime = dateTimePicker1.Value;
                    fvno = textBox1.Text;
                    genvoucher = new MySqlCommand("insert into cutting(vno,bno,nsize,machine,vdate,operator,stime,shift,status) values(@vno,@bno,@size,@machine,@date,@opt,@start,@shift,@status)", cn, genvouchrt);
                    genvoucher.Parameters.AddWithValue("@vno", fvno);
                    genvoucher.Parameters.AddWithValue("@size", textBox3.Text);
                    genvoucher.Parameters.AddWithValue("@bno", comboBox3.SelectedItem.ToString());
                    genvoucher.Parameters.AddWithValue("@machine", comboBox1.SelectedItem.ToString());
                    genvoucher.Parameters.AddWithValue("@date", tdat);
                    genvoucher.Parameters.AddWithValue("@opt", comboBox4.SelectedItem.ToString());
                    genvoucher.Parameters.AddWithValue("@shift", shift);
                    genvoucher.Parameters.AddWithValue("@start", ttime);
                    genvoucher.Parameters.AddWithValue("@status", "Open");
                    genvoucher.ExecuteNonQuery();
                    genvouchrt.Commit();

                    df = new MySqlCommand("insert into vouchers values(@vno,@dept,@shift,@task,@stats,@remarkn,@remarkv)", cn, dft);
                    df.Parameters.AddWithValue("@vno", textBox1.Text);
                    df.Parameters.AddWithValue("@dept", dept);
                    df.Parameters.AddWithValue("@shift", shift);
                    df.Parameters.AddWithValue("@task", "Cutting");
                    df.Parameters.AddWithValue("@stats", "Open");
                    df.Parameters.AddWithValue("@remarkn", "");
                    df.Parameters.AddWithValue("@remarkv", "");
                    df.ExecuteNonQuery();
                    dft.Commit();

                    MessageBox.Show("Voucher Opened Successfully!");
                    cn.Close();

                    this.Close();
                }
            catch (Exception eli)
            {
                MessageBox.Show("Can''t Generate Voucher" + Environment.NewLine + eli);
                try
                {
                    genvouchrt.Rollback();
                    dft.Rollback();
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Rollback Exception"+ex.Message);
                }
            }
        }

推荐答案

目前尚不清楚您想要什么. GOOGLE SEARCH 是您的朋友.因此,这是在 GOOGLE SEARCH 中找到的两个文档链接,其中还包括代码示例:
* SqlTransaction类(System.Data.SqlClient) [ ^ ]
* 交易和批量复制操作| Microsoft文档 [了解ADO.NET中的事务和TransactionScope的初学者教程 [ ^ ]

还有 GOOGLE SEARCH (更多搜索指南):
It is unclear what you want. GOOGLE SEARCH is your friend. So, here are a couple of documentation links, found with GOOGLE SEARCH, that also include code examples:
* SqlTransaction Class (System.Data.SqlClient)[^]
* Transaction and Bulk Copy Operations | Microsoft Docs[^]

Here is a tutorial found right here on CodeProject:
* A Beginner''s Tutorial for Understanding Transactions and TransactionScope in ADO.NET[^]

And a GOOGLE SEARCH for more tutorials: SqlTransaction beginner examples C#[^]


您询问以下内容是否相似:
You asked if the following are similar:
SqlConnection cn;
SqlTransaction st;
st=cn.BeginTransaction;


V/s


V/s

SqlConnection cn;
SqlTransaction st=new SqlTransaction();


但是,由于后一个问题尚未编译,因此该问题不太合理.换句话说,您不能直接实例化SqlTransaction类.这是因为构造函数定义为internal.

如果您尝试编译后面的代码,则会收到错误消息


However, the question does not quite make sense since the latter one does not compile. In other words, you cannot instantiate a SqlTransaction class directly. This is because the constructor is defined as internal.

If you try to compile the latter code you''ll get an error

Severity Code   Description
-------- ------ ----------------------------------------------------------------------
Error    CS1729 ''SqlTransaction'' does not contain a constructor that takes 0 arguments	


阻止直接实例化SqlTransaction类的原因可能是,在BeginTransaction调用期间,执行了一些检查并进行了分配,例如:
-如果未指定,则默认隔离级别设置为读取已提交
-连接已验证
-如果需要恢复连接并断开连接
-等等...


Probably the reason for preventing direct instantiation of SqlTransaction class is that during BeginTransaction call, several checks are carried out and assignments made, for example:
- default isolation level is set to read committed if not specified
- connection is validated
- the connection is restored if needed and broken
- and so on...


这篇关于在sqltransaction中是否类似的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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