问题描述
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);
}
}
}
推荐答案
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中是否类似的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!