C#从两个表中删除

问题描述:

你好,

我想使用C#从两个表中删除一条记录:Comanda和Furnizor与fk cod_furnizor.

Hello,

I want to delete a record from two tables : Comanda and Furnizor with fk cod_furnizor, using C#.

string delete_sql2 = "delete from comanda where cod_furnizor = :cod_furnizor";
string delete_sql1 = "delete from furnizor where cod_furnizor = :cod_furnizor";
OracleCommand cmd2 = new OracleCommand(delete_sql2, connG);
OracleCommand cmd = new OracleCommand(delete_sql1, connG);
cmd2.Parameters.Add(new OracleParameter(":cod_furnizor", Convert.ToInt32(cod)));
cmd.Parameters.Add(new OracleParameter(":cod_furnizor", Convert.ToInt32(cod)));
cmd2.ExecuteNonQuery();
cmd.ExecuteNonQuery();
cmd2.Dispose();
cmd.Dispose();



我试过了,但还是行不通.

[修改:您想使用< pre>标签不是< code>标签]



I tried this, but still doesn`t work.

[Modified: you wanted to use <pre> tags not <code> tags]

是否没有自动交易会导致在代码末尾回滚?

还是有例外?
Isn''t there an automatic transaction which will cause rollback at the end of code?

Or do you have an exception?


不,现在就这些了...
No, this is all for now ...
OracleConnection connG = conexiune.conn;
            try
            {
                foreach (ListViewItem itm in listView1.Items)
                {
                    string cod;
                    if (itm.Checked == true)
                    {
                        cod = itm.Text;
                        string delete_sql1 = "delete from furnizor f, comanda c where f.cod_furnizor = c.cod_furnizor and cod_furnizor = :cod_furnizor";
                        OracleCommand cmd = new OracleCommand(delete_sql1, connG);
                        cmd.Parameters.Add(new OracleParameter(":cod_furnizor", Convert.ToInt32(cod)));
                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }


voloda2是正确的:使用Begin Transaction,以便在发生任何故障时回滚.

这是取自 http://download.oracle.com/的示例docs/cd/B28928_01/doc/server.103/b28923/nvadonet.htm

voloda2 is right: Use Begin Transaction so that it will rollback if anything fails.

Here is an example taken from http://download.oracle.com/docs/cd/B28928_01/doc/server.103/b28923/nvadonet.htm

OracleConnection conn = new OracleConnection ("DSN=consroot;uid=system");
conn.Open();
IDbTransaction trans = conn.BeginTransaction(); // Turn off AUTOCOMMIT
OracleCommand cmd = (OracleCommand)conn.CreateCommand();
cmd.CommandText = "create table TEST1 (c0 number)";
cmd.ExecuteNonQuery();
trans.Commit(); // AutoCommit is ''ON''
cmd.Dispose();
conn.Close();



它在文章中指出,默认情况下它设置为自动提交,这也是我在mysql上的经验.这样可以避免这种情况的发生.



It states in the article that by default it is set to auto commit which has been my experience with mysql as well. This will prevent that from happening.