?
Solved

Visual Studio - MySQL Connection

Posted on 2010-08-20
8
Medium Priority
?
976 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

801 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