1. Home
  2. Database Software
  3. Help Connecting To Mysql Database With Visual Basic?

Discuss in my Forum

Help Connecting To Mysql Database With Visual Basic?

By admin Posted in: Database Software

Title pretty much explains what i’m trying to do, this is what I have:
Imports mysql.data.mysqlclient
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As MySqlConnection
conn = New MySqlConnection
conn.ConnectionString = “Server=localhost; Database=**7439db3; User id=**7439; Password=4862**”
Try
conn.Open()
Catch myerror As MySqlException
lblstatus.Text = “Error”
End Try
Dim myAdapter As New MySqlDataAdapter
Dim sqlquery = “Test Complete”
Dim myCommand As New MySqlCommand
myCommand.Connection = conn
myCommand.CommandText = sqlquery
myAdapter.SelectCommand = myCommand
Dim mydata As MySqlDataReader
mydata = myCommand.ExecuteReader
If mydata.HasRows = 0 Then
txtoutput.Text = “1″
Else
txtoutput.Text = “2″
End If
End Sub
End Class
but for some reason its not connecting to the database, any ideas?

  1. Anonymous Says

    You have no SQL query.
    Try this method
    Imports System.Data.SqlClient
    Public Class Form1 Inherits System.Windows.Forms.Form
    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    Dim dr As New SqlDataReader()
    ‘declaring the objects
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
    Handles MyBase.Load
    myConnection = New SqlConnection(“server=localhost;uid=sa;p…
    ‘establishing connection. you need to provide password for sql server
    Try
    myConnection.Open()
    ‘opening the connection
    myCommand = New SqlCommand(“Select * from discounts”, myConnection)
    ‘executing the command and assigning it to connection
    dr = myCommand.ExecuteReader()
    While dr.Read()
    ‘reading from the datareader
    MessageBox.Show(“discounttype” & dr(0).ToString())
    MessageBox.Show(“stor_id” & dr(1).ToString())
    MessageBox.Show(“lowqty” & dr(2).ToString())
    MessageBox.Show(“highqty” & dr(3).ToString())
    MessageBox.Show(“discount” & dr(4).ToString())
    ‘displaying the data from the table
    End While
    dr.Close()
    myConnection.Close()
    Catch e As Exception
    End Try
    End Sub
    End Class

  2. Gardner Says

    Step through the code in debug mode. If it’s throwing an error when you try to connect, you can put your mouse over the myerror var and see exactly what the problem is.

Leave a Reply

You must be logged in to post a comment.

More Interesting Things

©2011 Softwares Reviews, All rights reserved.