Saturday, March 3, 2018

LINQPAD - How to Insert data into MySQL using VB

This snippet connect to MySQL in LINQPAD using Visual Basic. Make sure you download the MySql.Data.DLL, unblock the zip file then extract it and add that reference in LINQPAD by pressing F4 as shown in the video here

Main sub

Sub Main
 Dim query As String
 Dim stid As Integer
 Dim name, programme As String
 stid = 13
 name = "Nemo"
 programme = "Search of Nemo"
 query ="insert into Student(Stid,Name,Programme) values (" & stid & ",'" & name & "','" &  programme & "')"
 insertInDB (query)
End Sub

  

Insertion sub



Sub InsertInDB (query As String)

      'Declaring variables
     Dim conn As New MySqlConnection()
  Dim sqlCmd As MySqlCommand 
  'Connection string to MySQL
        conn.ConnectionString = "Persist Security Info=False;database=springboot;server=localhost;Connect Timeout=30;user id=springboot; pwd=springboot"
        Try
      'Connecting and executing the query
            conn.Open()
   sqlCmd = New MySqlCommand( query, conn)
   sqlCmd.ExecuteNonQuery()
   'Closing the query
   conn.Close()
        Catch myerror As MySqlException
  'Throwing exceptions
            Console.WriteLine("Error connecting to database!")
            Exit Sub
        End Try
        Console.WriteLine("connected to database!")
End Sub
  

This is just a trick, hope it paved your way out. 

No comments:

Post a Comment