1

stuck in trouble. I have a txt-file, which save SQL Commands, when Internet Connection is disabled. enter image description here

The fact is that in my database table there is a primary key marked on the date column. And sometimes SQL Command is corrupted( like on picture) and the SQL command loops on a specific date. It must be done so that if he cannot send a command, skip it and move on to the next command.

The part of my code, which send strings as SQL Commands :

If _DB.State = ConnectionState.Open Then
                    Dim Massiv() As String = IO.File.ReadAllLines("C:\Components\Data.txt", System.Text.Encoding.Default)
                    For Each element In Massiv

                        _SQLcommandFromFiles = New SqlCommand(element, _DC)
                        _SQLcommandFromFiles.ExecuteNonQuery()
                    Next
3
  • There's something wrong with your code. As you have chosen not to share the relevant code with us, we can't possibly tell you what's wrong with it. Commented Feb 6, 2019 at 5:34
  • 1
    Did you analyse where and why this crippled commands are produced? In first place you should not find a solution to handle crippled commands, instead you should find a solution that avoids producing crippled commands at all. ((and because i love metaphors) Actual you asked for a automated tire hole patching, instead of avoiding driving thru nails at all.) Commented Feb 6, 2019 at 5:35
  • @Horitsu Thank you for responding. I guessed that SQL Command write wrong SQLCommand, without my data, into my txt file, when my connection to database is reconnected. Commented Feb 6, 2019 at 6:24

2 Answers 2

1

You should surround your code with try catch block like this

If _DB.State = ConnectionState.Open Then
                    Dim Massiv() As String = IO.File.ReadAllLines("C:\Components\Data.txt", System.Text.Encoding.Default)
                    For Each element In Massiv
                         Try
                            _SQLcommandFromFiles = New SqlCommand(element, _DC)
                            _SQLcommandFromFiles.ExecuteNonQuery()
                         Catch ex As Exception

                         End Try
                    Next
Sign up to request clarification or add additional context in comments.

Comments

0

You should open your file then read line by line and use 'try catch' for the execution of your query.

1 Comment

Welcome to the community :) Make sure you include as much details as possible or code snapshots before answering !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.