Solved

Visual Studio - MySQL Connection

Posted on 2010-08-20
8
951 Views
Last Modified: 2013-11-27
Good morning,

I am using Visual Studio 2005 (coding in VB) and am trying to get a connection to my MySQL Connection, using ADODB (as I have used this before and am familiar with the workings)

However when I DIM my connection string

     Dim MyADOConnection As MySqlConnection

I get an error

     Type 'MySQLConnection' is not defined

I presume I am missing a reference, but cannot find out where to put this in. Can somebody please point me in the right direction.

Many thanks
0
Comment
Question by:Dan560
8 Comments
 
LVL 6

Expert Comment

by:Rajesh_mj
ID: 33483217
Add reference using the Add reference option.This will get when u right click on the reference which comes in the Soultion Explorer
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33483223
Did you install mySql .NET Connector ?

VB.net to mySQL Database Connection
http://www.codeproject.com/KB/vb/VBnet_to_mySQL_Server.aspx
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 33483226
I have a reference to C:\Program Files\MySQL\MySQL Connector Net 5.0.9\Binaries\.NET 2.0\MySql.Data.dllusing MySql.Data.MySqlClient;in the codethat is c# you need the equivalent vb
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 33483236
You can also use the OldeDbConnection


Source: http://p2p.wrox.com/pro-vb-net-2002-2003/5338-connecting-mysql-vbulletin-net.html
Now, assuming you've installed the MyOLEDB driver and your MySQL server is up and running, let's begin.  I only cover VB.NET code, but the methodology is similar for C# as well.  
							
Dim fdCon As New OleDb.OleDbConnection("Provider=MySQLProv;Data Source=DATABASE;User Id=YOURSQLUSERNAME;Password=YOURSQLPASSWORD;")
Dim fdCom As New OleDb.OleDbCommand("SELECT * FROM TABLENAME ORDER BY DESIREDFIELD ASC", fdCon)
fdCom.Connection.Open()
Dim fdRead As OleDb.OleDbDataReader = fdCom.ExecuteReader(CommandBehavior.CloseConnection)
While fdRead.Read
     'Do what you want here.  Below is code that will pop up a message box for every record.
     'This code would work if your table had three fields.
     'This database's first field is an auto-incrementing ID medium integer, second field is a
     'VarChar, and the third is also a VarChar.  This code displays each field on its own line.
     MsgBox(fdRead.GetValue(0) & vbCrLf & fdRead.GetValue(1) & vbCrLf & fdRead.GetValue(2))
End 
While

fdCon.Close()

Open in new window

0
 
LVL 14

Expert Comment

by:Kalpan
ID: 33483238
You can either use ODBC or MySql Client for .NET.

 

1. Download MySql Client dll.

2. Add the dll to your project.

3. Add connection string to web.config

<connectionStrings>
    <add name="Connstring" connectionString="server=localhost; userid=uid;password=pwd;pooling=yes;Database=DB" providerName="MySql.Data.MySqlClient"/>
  </connectionStrings>
  <system.data>
    <DbProviderFactories>
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=5.0.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
    </DbProviderFactories>
  </system.data>
0
 
LVL 2

Author Comment

by:Dan560
ID: 33483544
I am having no joy in trying to add the mysql client dll to visual studio in that it errors with a message saying that it can not add a component......
Therefore I am following DHaest's suggestion and trying the OLEdbconnection.
My database is on a remote server called TestServer and the database is called SWTEST
I know the conenction name and password so can somebody please let me know what the connection string would be (I am struggling on where to put the server name).

"Provider=MySQLProv;Data Source=DATABASE;User Id=YOURSQLUSERNAME;Password=YOURSQLPASSWORD;")
Many thanks for your help
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33483580
A very good resource for connectionstrings:
http://www.connectionstrings.com/

This is just one connection string sample for the wrapping OleDbConnection class that calls the underlying OLEDB provider. See respective OLE DB provider for more connection strings to use with this class.
Provider=MySQLProv;Data Source=mydb;User Id=myUsername;Password=myPassword;

This is just one connection string sample for the wrapping OdbcConnection class that calls the underlying ODBC Driver. See respective ODBC driver for more connection strings to use with this class.
Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase; User=myUsername;Password=myPassword;Option=3;
0
 
LVL 14

Expert Comment

by:raja_ind82
ID: 33498987
check connection Strings in this following links:
http://www.connectionstrings.com/?carrier=mysql

Hi, you can conenct MYSQL using ODBC connection.  

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim Server As String = "localhost"
        Dim DataBaseName As String = "test"
        Dim User As String = "root"
        Dim Password As String = ""

        'Preparation for Connect string
        Dim ODBCConnectString As String = _
            "DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & Server _
            & ";DATABASE=" & DataBaseName & ";UID=" & User _
            & ";PASSWORD=" & Password & ";OPTION=3;"

        'Open Database
        Dim ConnectionObject As New System.Data.Odbc.OdbcConnection(ODBCConnectString)
        Try
            'Prepare query to insert record
            Dim svlQuery As String = "insert into admin values('raja','raja')"

            'To open command object to execute query
            Dim CommandObject As New System.Data.Odbc.OdbcCommand(svlQuery.ToString.Trim, ConnectionObject)
            ConnectionObject.Open()

            'To Execute query
            CommandObject.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox("Error Occured :" & vbCrLf & ex.Message)
        End Try
        'To close the opened databse
        ConnectionObject.Close()
        ConnectionObject = Nothing
    End Sub
************************************************************************
Please have a look at this VB.NET MYSQL tutorials
http://www.vbmysql.com/articles/vbnet-mysql-tutorials/the-vbnet-mysql-tutorial-part-3/

ODBC driver:
http://dev.mysql.com/downloads/connector/odbc/3.51.html

Without Driver is little bit difficult. Anayway gothrough this thread.
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_DB/Q_20836152.html

Regards,
M.Raja
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question