Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

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

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


I tried this, but still doesn`t work.

[Modified: you wanted to use <pre> tags not <code> tags]
Posted
Updated 27-May-10 11:36am
v3

Isn't there an automatic transaction which will cause rollback at the end of code?

Or do you have an exception?
 
Share this answer
 
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());
            }
 
Share this answer
 
v2
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();


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.
 
Share this answer
 
Try this:

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";
IDbTransaction trans = connG.BeginTransaction();
OracleCommand cmd = new OracleCommand(delete_sql1, connG);
cmd.Parameters.Add(new OracleParameter(":cod_furnizor", Convert.ToInt32(cod)));
cmd.ExecuteNonQuery();
cmd.Dispose();
trans.Commit();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900