VB.net SQL Fetch thousands of values without Transact Rollback

Hi
I want to use code similar to the following code to fetch thousands of values from my database
with a SQL statement per value. I don't want to use a Transact Rollback Query but would rather open
the connection once then if one of the SQL statements fails just ignore that and move onto the next.
How would I achieve that? Thanks.

           Dim connection As New SqlConnection(oConnectionString)
                    Dim cmd As New SqlCommand(sSQL, connection)
                    cmd.CommandTimeout = oTimeOut

                    connection.Open()
                    Get_Primary_Key = cmd.ExecuteScalar().ToString
                    connection.Close()

Open in new window

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
 
Kyle AbrahamsSenior .Net DeveloperCommented:
If you really need to fire a sql statement off a number of times:
Dim connection As New SqlConnection(oConnectionString)
connection.Open()

dim i as int = 0
while i < 1000
  Dim cmd As New SqlCommand(sSQL, connection)
 cmd.CommandTimeout = oTimeOut
 Get_Primary_Key = cmd.ExecuteScalar().ToString
wend

connection.Close()

Open in new window


However you can also do a select * from table with a dataReader and load it into the dataTable.
0
 
Nitin SontakkeDeveloperCommented:
All you have to do is encapsulate the call in some other function may be passing open connection object as a parameter. Apply try catch to ignore the failed calls, if you wish. The fact that you are reading the values, transaction doesn't come much in the picture any way.

Having said that, I personally believe that you are better of off-loading all the logic on the server side, prepare all your 'thousands of values' probably in a stored procedure and call the stored procedure in application to fetch all of the values in one go.

Imagine the network traffic saved.
1
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thank you both
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.