Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi guys,

I tried to add a transaction when I insert row by row from a datatable into my database, here is my code:

public string addNewItems(DataView pDataView, string pTableName, int pTableId, string pUserName)
     {
         string returnString = string.Empty;
         Database db = AseDatabase;

         using (DbConnection connection = db.CreateConnection())
         {
             connection.Open();
             DbTransaction transaction = connection.BeginTransaction();
             //OdbcParameter parameter;
             DbCommand cmd;
             try
             {
                 cmd = db.GetSqlStringCommand("SET CHAINED OFF");
                 db.ExecuteNonQuery(cmd);

                 foreach (DataRowView pRowView in pDataView)
                 {
                    //I build the string sqlcmd

                    //cmd = db.GetSqlStringCommand(sqlcmd);

                    // I add parameters

                    // I execute the cmd in transaction:
                    // db.ExecuteNonQuery(cmd, transaction);


                 }

                 transaction.Commit();
             }
             catch (Exception ex)
             {
                 returnString = ex.Message;
                 transaction.Rollback();
                 throw;
             }

             return returnString;
         }
     }


The problem comes from the fact that when I try to insert, it shows an error :

ERROR [ZZZZZ][Sybase][ODBC Driver][Adaptive Server Enterprise]The résidente SQL procedure 'psi_spy' may be run only in unchained transaction mode. The 'SET CHAINED OFF' command will cause the current session to use unchained transaction mode.	


It is really strange, since when I tried to run my code with a simple created test table, it goes well. But the below error shows just when I run my code with my working tables...

I also checked the option insert of my working table, it seems it works well...

Could anyone help me please?



P.S:

As you can see, I "set chained off" below.

The real problem is, if I set chined off in the transaction like this:

C#
cmd = db.GetSqlStringCommand("SET CHAINED OFF");
                    db.ExecuteNonQuery(cmd,transaction);


then the transaction just doesn't work..

if I set chined off outside the transaction like this:

C#
cmd = db.GetSqlStringCommand("SET CHAINED OFF");
                    db.ExecuteNonQuery(cmd);

then the error just shows...



thank you in advance!!!
Posted
Updated 19-Apr-13 5:36am
v3

Hello,

The error is reported if a stored procedure is created with chained mode switched off. You need to then execute the stored procedure with chained mode switched off too.

Check this thread: http://www.sybaseteam.com/set-chained-off-t-2092.html

good luck
jafc
 
Share this answer
 
Comments
jessicachen12 19-Apr-13 11:33am    
Thank you for replying me! In fact, I tried to run a SqlStringCommand not a stored porcedure...Also, as you can see, I did "set chained off" below. The real problem is, if I set chined off in transaction, the transaction just doesn't work, and if I set chined off outside the transaction, the errors then shows...
This problem comes from a stored procedure need to be run at unchained mode. The best way to solve this problem is to set the proc "anymode" directly in the database like this:

>exec sp_procxmode <stored proc="" name="">, "anymode"
>GO


Please be sure of that if everything is ok when the "anymode" is setted.
 
Share this 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