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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thank you both
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.