views:

86

answers:

1

I am unable to rollback using the following code snippet and need help:

import clr
import sys
clr.AddReference('System.Data')
from System.Data.SqlClient import SqlConnection, SqlParameter, SqlTransaction
conn_string = "****"
connection = SqlConnection(conn_string)
connection.Open()
createuser = connection.CreateCommand()
createuser.CommandText = "****"
try:
 reader = createuser.ExecuteReader()
 reader.Close()
except:
 reader.Rollback()

Thanks!

I understand now, however now I'm having a problem where its expecting a string but I cant do the parametrized values if I put the SQL query string in as the argument:

createuser.CommandText = "****"
createuser.Parameters.AddWithValue("@Username", username);
usertransaction = connection.BeginTransaction(createuser)
try:
    usertransaction.Commit()
except:
    usertransaction.Rollback()
A: 

You need to begin a transaction using SqlConnection.BeginTransaction() method. You'll receive a transaction object having methods Commit and Rollback.

elder_george
see my edit please... thanks!
Sorry, I don't understand.. You surely can (and should) use parameters in given scenario. Yes, `DbParameterCollection` doesn't have `AddWithValue` method, but you can call `CreateParameter` on your command, set it up (type, name, direction, value etc) and then `Add` it to command's `Parameters`. This is a bit cumbersome, but not hard at all... Or I misunderstood and you've meant something else?
elder_george