Earn Extra Money

Sample Program of VB.net Connected to MySQL


Imports MySql.Data.MySqlClient

Public Class Form1

Dim ServerString As String = "Server=localhost;User Id=root;Password='';Database=youtube"

Dim SQLConnection As MySqlConnection = New MySqlConnection





Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load







SQLConnection.ConnectionString = ServerString





Try

If SQLConnection.State = ConnectionState.Closed Then

SQLConnection.Open()

MsgBox("Successfully connected to mysql database")

Else

SQLConnection.Close()

MsgBox("Connection Failed")





End If

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub

Public Sub SaveNames(ByRef SQLStatement As String)

Dim cmd As MySqlCommand = New MySqlCommand

With cmd

.CommandText = SQLStatement

.CommandType = CommandType.Text

.Connection = SQLConnection

.ExecuteNonQuery()





End With





SQLConnection.Close()

MsgBox("Successfully Added")

SQLConnection.Dispose()





End Sub





Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click

Dim SQLStatement As String = "INSERT INTO people(name) VALUES('" & txtName.Text & "')"





SaveNames(SQLStatement)

End Sub





Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

txtName.Text = " "





End Sub

End Class

4 comments:

  1. how about connecting it into oracle??

    ReplyDelete
    Replies
    1. Imports System.Data
      Imports System.Data.OracleClient

      Sub Main()
      Dim connString As String = "server = oracleserver; uid = admin;password = password;"

      'Create connection
      Dim con As New OracleConnection(connString)

      Try
      con.ConnectionString = connString
      con.Open()
      Console.WriteLine("Connected to Oracle.")

      Catch ex As OracleException
      Console.WriteLine("Error: " & ex.ToString())
      Finally
      ' Close Connection
      con.Close()
      Console.WriteLine("Connection Closed")

      End Try

      End Sub

      transfer it to form. its same in mysql

      Delete