This is a migrated thread and some comments may be shown as answers.

Status returned from SQL command

3 Answers 79 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Nigel
Top achievements
Rank 2
Nigel asked on 18 Jun 2012, 09:05 AM
Hi,

Is it possible to capture the value returned from executing a SQL statement executed through the Code Behind window?
I have a script that works fine, and the VB in the code behind window also works fine.  However, I want to expand the VB script if possible, so that I can execute the following algorithm:

Open connection to database
Execute SQL statement to check if value exists in a database table
If the value exists then
    Do this
Otherwise
    Do this
End If
Close connection to database

What I need to understand is how to get a result back from an execution statement such as:
   
     thisCommand.CommandText = "SELECT myValue FROM DatabaseTable"
     thisCommand.ExecuteNonQuery()

Is this possible?  Is there a better way?  If so, what do I need to do to make it happen?

When I run the following code, the value returned to my variable aResult is always -1, regardless of whether the search succeeds or not:

SQL_Command = "SELECT * FROM LookupItemsTexts WHERE [Text] = 'fred'"
Log.WriteLine(SQL_Command)  'debug line
'Now execute the command
thisConnection.Close()    
thisConnection.Open()
thisCommand.CommandText = SQL_Command
aResult = thisCommand.ExecuteNonQuery()
Log.WriteLine(aResult.ToString())  'debug line

Thanks,
Nigel Edwards.

3 Answers, 1 is accepted

Sort by
0
Accepted
Anthony
Telerik team
answered on 18 Jun 2012, 04:21 PM
Hello Nigel,

Unfortunately we won't be able to troubleshoot this for you, as this code is not specific to Test Studio. The best we can provide is this basic article to get you started, which you may have already seen.

Kind regards,
Anthony
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Nigel
Top achievements
Rank 2
answered on 19 Jun 2012, 07:48 AM
Okay, thanks Anthony.

Has ANYONE in the community any experience of running SQL from the code behind window (VB or C)?  I must assume that the Telerik libraries don't have any function that picks up and returns a value from SQL for an executed query.

If not then this might be something that the Telerik team should consider implementing in a future Test Studio release.  After all, SQL is (I think) the most widely used DB language in use today.
0
Nigel
Top achievements
Rank 2
answered on 21 Jun 2012, 11:04 AM
I believe I have found the answer i wanted in a Telerik on-line documentation page:

http://www.telerik.com/automated-testing-tools/support/documentation/user-guide/code-samples/general/access-sql-database.aspx

The code on that page shows me how to check for the existence of a data item in the database:
Log.WriteLine("Value of City column: " + DirectCast(thisReader("City"), [String]))
Although I can't explicitely use code such as 
    aResult = thisCommand.ExecuteNonQuery()
to verify that a command has executed correctly, I can use a form of the first snippet to capture a value and prove that the command has executed by checking the database to see if a value still exists (or doesn't) after the command has been executed.

Thanks,Nigel.
Tags
General Discussions
Asked by
Nigel
Top achievements
Rank 2
Answers by
Anthony
Telerik team
Nigel
Top achievements
Rank 2
Share this question
or