0

Data upload through .sql script file using vb.net

I need to upload data through SQL script file to mySQL using vb.net windows application. Following code I had written that is working but unable to capture any error if there is any mistake in SQL script (if that SQL script which containg plain text).

How can I check that SQL script runs without any error.

Sample script file.

insert into merhiermaster(`mercatid`,`mercatdesc`,`hsncode`,`active`) values('100','Books','49011010','1');
insert into merhiermaster(`mercatid`,`mercatdesc`,`hsncode`,`active`) values('300','Collaterals','48191010','1');
insert into merhiermaster(`mercatid`,`mercatdesc`,`hsncode`,`active`) values('400','Gifts','09024020','1');
insert into merhiermaster(`mercatid`,`mercatdesc`,`hsncode`,`active`) values('400','Gifts','33030010','1');
insert into merhiermaster(`mercatid`,`mercatdesc`,`hsncode`,`active`) values('400','Gifts','33049930','1');
insert into merhiermaster(`mercatid`,`mercatdesc`,`hsncode`,`active`) values('400','Gifts','42021110','1');

Function for uploading data

Private Function LoadSQLFile(ByVal pfileinitial) As Boolean
        Try
            Dim server As String = gblServer
            Dim user As String = gblDataBaseUserId
            Dim password As String = gblPassword
            Dim database As String = gblDatabase
            Dim port As String = "3306"
            Dim process = System.Diagnostics.Process.Start(New ProcessStartInfo With {
        .FileName = "mysql.exe",
        .Arguments = String.Format("-C -B --host={0} -P {1} --user={2} --password={3} --database={4} -e ""\. {5}""", server, port, user, password, database, pfileinitial),
        .ErrorDialog = True,
        .CreateNoWindow = False,
        .UseShellExecute = False,
        .RedirectStandardError = True,
        .RedirectStandardInput = True,
        .RedirectStandardOutput = True,
        .WorkingDirectory = Environment.CurrentDirectory
    })
            process.Start()
            process.BeginErrorReadLine()
            process.BeginOutputReadLine()
            process.StandardInput.Close()
            process.WaitForExit()


            Return True
        Catch ex As Exception
            MsgBox(ex.Message.ToString, MsgBoxStyle.Information, gblMessageHeading)
            Return False
        End Try
    End Function
Ashish
  • 19
  • 5
  • For how to capture output (including error output) when using [System.Diagnostics.Process](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=net-6.0), see the following post: https://stackoverflow.com/a/74005878/10024425 – Tu deschizi eu inchid Nov 09 '22 at 15:16

0 Answers0