如何引用对象并将其传递给C#中的方法? [英] How to reference object and passing it to method in C#?

查看:80
本文介绍了如何引用对象并将其传递给C#中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

Here is My Code:

private void btnsend_Click(object sender, EventArgs e)
       {
           cn.Open();//Conncetion

           try
           {
               var size = textBox3.Text;
               var machine = comboBox1.SelectedItem.ToString();
               MySqlCommand cmd = new MySqlCommand("select kgs from capacity_master where(nsize=@size and mname=@machine)", cn);
               cmd.Parameters.AddWithValue("@size", size);
               cmd.Parameters.AddWithValue("@machine", machine);
               using (MySqlDataReader sf = cmd.ExecuteReader())
               {
                   while (sf.Read())
                   {
                       oload = double.Parse(sf[0].ToString());
                   }
               }
           }
           catch (Exception eli)
           {
               MessageBox.Show("" + eli);
           }

           MySqlTransaction el = cn.BeginTransaction();//Transaction Started From Here

           try
           {
               string fvno;
               dateTimePicker1.Value = DateTime.Now;
               dateTimePicker2.Value = DateTime.Now;
               var tdat = dateTimePicker2.Value;
               var ttime = dateTimePicker1.Value;
               fvno = textBox1.Text;

               MySqlCommand genvoucher = new MySqlCommand("insert into htemp(vno,bno,nsize,machine,loadqty,overload,vdate,operator,stime,shift,status) values(@vno,@bno,@size,@machine,@load,@oload,@date,@opt,@start,@shift,@status)", cn,el);
               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("@load", textBox14.Text);

               var canload = oload;
               var doload = double.Parse(textBox14.Text);
               string ans;
               if (doload > canload)
               {
                   ans = "Yes";
               }
               else
               {
                   ans = "No";
               }
               genvoucher.Parameters.AddWithValue("@oload", ans);

               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();

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

               // Method To Do Other SQL Transaction
                  batchprocess2();

               MessageBox.Show("Voucher Opened Successfully!");
               el.Commit();//Transaction Committed Here
               this.Close();
           }
           catch (Exception eli)
           {
               MessageBox.Show("Can't Generate Voucher" + Environment.NewLine + eli);
               try
               {
                   el.Rollback();//Rollbacked if Exception any
               }
               catch(Exception ea)
               {
                   MessageBox.Show("Rollback Exception"+ea.Message);
               }
           }
           cn.Close();
       }

       public void batchprocess2()
       {
           try
           {
               var lqty = float.Parse(textBox14.Text);
               var ti = float.Parse(textBox31.Text);
               var tw = float.Parse(textBox29.Text);
               var fti = ti - lqty;
               var ftw = tw + lqty;
               MySqlCommand opp = new MySqlCommand("update batch_process set tem_in=@ti,tem_wait=@tw where batch_no=@nbno", cn);
               opp.Parameters.AddWithValue("@nbno", comboBox3.SelectedItem.ToString());
               opp.Parameters.AddWithValue("@ti", fti);
               opp.Parameters.AddWithValue("@tw", ftw);
               opp.ExecuteNonQuery();
           }
           catch (Exception eli)
           {
               MessageBox.Show("Can't update batch" + eli);
           }
       }





当发生异常时,事务从其开始的位置回滚提交的地方。

但是我在这个交易之间创建了一个方法,我想在主要交易中包含该方法交易。



i想要回滚并提交



When exception occurs the transaction is rollbacked from where it was began to where it was committed.
But i have created one method between this transaction and i want to include that method transaction in main transaction.

i want rollback and commit all transaction between

BeginTransaction()

Commit()



但是我无法引用MySqlTransaction的对象,我该怎么做?



我试过的:



我只是试图通过参考使用MysqlTransaction对象进入名为batchprocess2的方法。

我想将所有查询都包含在主要事务中。



如何可能或不是?


But i can't refrence the object of MySqlTransaction, How can i do this?

What I have tried:

Simply i trying to use MysqlTransaction object by refrence into my method named "batchprocess2".
I want to include all queries into main transaction.

How it is possible or it's not?

推荐答案

除非你需要将Transaction对象传递给你的方法期望该方法提交或回滚事务 - 和t帽子会是一个坏主意,因为该方法对其外部发生的事情一无所知。



而不是传递事务,在catch块中抛出一个新的异常包含 eli 异常作为InnerException,让你的普通 try ... catch 块在外部方法处理它和回滚交易:

You don't need to pass the Transaction object to your method unless you expect that method to Commit or Rollback the transaction - and that would be a bad idea, because the method knows nothing about what is happening outside it.

Instead of passing the transaction, in your catch block throw a new exception which includes the eli exception as an InnerException and let your normal try...catch block in the outer method handle it and rollback the transaction:
catch (Exception eli)
{
    throw new ApplicationException("Can't update Batch", eli);
}


我的理解是你可以堆叠 SQLTransaction [ ^ ]但是如果外部SqlTransactions回滚,那么内部的回滚。因此,如果您希望数据库更改不受SqlTransaction Rollback的影响,那么这些操作必须存在于外部,否则它们也将被回滚。
My understanding is that you can have stacked SQLTransaction[^] however if outer SqlTransactions rollback, so do the inner ones. So if you want DB changes not to affected by the SqlTransaction Rollback, then those actions must exist outside otherwise they will be rolled back also.


这篇关于如何引用对象并将其传递给C#中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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