CoachHawk Posted June 21, 2019 Posted June 21, 2019 Hello: I got the db connection working to Sql Server and use _Sql_GetTableAsString() to get results from select. However, when doing an update I think I should use _SQL_Execute() instead of _Sql_GetTableAsString(). But while the code DOES UPDATE the value in the db, it does NOT branch to show the 'Complete' msgbox, instead it branches to ConsoleWrite('Error... but I get an empty _SQL_GetErrMsg(). I've tried WRAPPING the SQL with BEGIN TRANSACTION ... COMMIT TRANSACTION but get same result. Am I correct to use _SQL_Execute() for a db Update? How do I determine the error? i.e. How do I get error text? ; from llewxam ; at https://www.autoitscript.com/forum/topic/132501-connecting-to-ms-sql-server-and-querying-a-table-from-a-db/ #include <_sql.au3> Local $ServerAddress = "xx.xx.xx.xx" Local $ServerUserName = "user" Local $ServerPassword = "pass" Local $DatabaseName = "dbname" Local $Sql $Sql = "update tab set col1 = 3.14 where id = 42;" _SQL_RegisterErrorHandler() ;register the error handler to prevent hard crash on COM error $OADODB = _SQL_Startup() If $OADODB = $SQL_ERROR Then MsgBox(0 + 16 + 262144, "Error", _SQL_GetErrMsg()) If _sql_Connect(-1, $ServerAddress, $DatabaseName, $ServerUserName, $ServerPassword) = $SQL_ERROR Then MsgBox(0 + 16 + 262144, "Error 1", _SQL_GetErrMsg()) _SQL_Close() Exit EndIf If _SQL_Execute(-1, $Sql) = $SQL_OK Then ; this runs field is updated, but script is going to ELSE branch MsgBox(48, 'Sql Update', 'Complete') Else ConsoleWrite('Error is: ' & _SQL_GetErrMsg() & @CRLF) ; Branching to here, but update successful and no text error returned EndIf Thanks for the help!
CoachHawk Posted June 24, 2019 Author Posted June 24, 2019 It looks like _SQL_Execute() returns a null. The documentation states that " On Success - Returns a query handle", but not even sure what that is. But in any event, I'm all set 😎. I'm new to AutoIT and appreciate this forum so thanks to everyone who makes it a success.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now