Solved

Visual Studio - MySQL Connection

Posted on 2010-08-20
8
929 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Expert Comment

by:Kalpan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now