Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
how to add a sql transaction if I have the procedure, and in one of these procedures, failed to execute sql ..

if one fails in execution of the sql sql in another procedure is not executed

ex :

 in procedure 1 :     string sql = "delete from column where id='" + id+ "' and column_name='" + columnname + "'"; mda.execSQL(sql1, connStr);
in procedure 2 : mda.execSQL(sql2, connStr);   string sql = "delete from columntype where idtype='" + id+ "' and column_name='" + columnname + "'"; mda.execSQL(sql, connStr);
in procedure 3 : mda.execSQL(sql2, connStr);   string sql = "delete from columntype where idtype='" + id+ "' and column_name='" + columnname + "'"; mda.execSQL(sql, connStr);

please check also my DataAccess.cs 

C#
public string execSQL(String queryString, String ConnectionString)
    {

        // Retrieve the connection string stored in the Web.config file.

        SqlConnection connection = new SqlConnection(ConnectionString);
        SqlCommand  cmd = new SqlCommand( queryString,connection  );

        try
        {
            // Connect to the database and run the query.
            
            connection.Open();            
            cmd.ExecuteNonQuery ();

            // Fill the DataSet.
            return "sukses";

        }
        catch (Exception ex)
        {

            // The connection failed. Display an error message.
            return  ex.Message;

        }

        

    }
Posted
Comments
Kornfeld Eliyahu Peter 8-Dec-14 5:38am    
See the sample in the link how to assign transaction to one or more SQL commands...
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction%28v=vs.110%29.aspx
Sinisa Hajnal 8-Dec-14 6:28am    
Why didn't you search for the solution first? Easily found.

1 solution

Add a Transaction to your C# code:
C#
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
    using (SqlCommand  cmd = new SqlCommand( queryString,connection  ))
    { 
        SqlTransaction trans;
        try
        {
            connection.Open();  
            trans = connection.BeginTransaction("Trans");
            cmd.Transaction = trans;
            cmd.ExecuteNonQuery ();
            trans.Commit();
            return "sukses";
        }
        catch (Exception ex)
        {
            if trans != null) trans.Rollback();
            return  ex.Message;
        }
    }
}


[edit]:doh: forgot to connect the transaction to the command... :O [/edit]
[edit]Griff, get yourself a coffee - you need it...[/edit]
 
Share this answer
 
v3
Comments
Kornfeld Eliyahu Peter 8-Dec-14 5:50am    
You missed this me think:
cmd.Transaction = trans;
OriginalGriff 8-Dec-14 6:03am    
Eee! That was stooopid of me...fixed. :thumbsup:
Member 11165607 8-Dec-14 6:08am    
Error list : The name 'trans' does not exist in the current context
OriginalGriff 8-Dec-14 6:17am    
Really, you should be able to fix that yourself - it's a trivial scope problem - but I've fixed the answer.

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