tags:

views:

26

answers:

1

Hi,

I am trying connect to MySQL database from ASP.net code. some my connection is not working.

for the below code I am getting invalied argument DRIVER. Can you help me to correct this.

Or show me some sample code.

Label1.Text = ""
        Label2.Text = ""
        Try
            Dim conStr As New SqlClient.SqlConnection
            conStr.ConnectionString = "DRIVER={MySQL ODBC 5.1 Driver};" +
                         "SERVER=localhost;" +
                         "DATABASE=aa;" +
                         "UID=aa;" +
                         "PASSWORD=aa;" +
                         "OPTION=3"

            Response.Write("Connection string:  " & conStr.ConnectionString)
            conStr.Open()
            If conStr.State = ConnectionState.Open Then
                Label1.Text = "SQLConnection conStr is Open"
                conStr.Close()
            ElseIf conStr.State = ConnectionState.Closed Then
                Label1.Text = "SQLConnection conStr is closed"
            End If
        Catch sqlxcp As SqlClient.SqlException
            Label2.Text = sqlxcp.ToString

        Finally
        End Try

Thanks

+1  A: 

SqlClient is for SQL Server. With the connection string above you need to use the OBBC provider classes (from here):

using Microsoft.Data.Odbc;

namespace myodbc3
{
  class mycon
  {
    static void Main(string[] args)
    {
      try
        {
          //Connection string for Connector/ODBC 3.51
          string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
            "SERVER=localhost;" +
            "DATABASE=test;" +
            "UID=venu;" +
            "PASSWORD=venu;" +
            "OPTION=3";

          // Connect to MySQL using Connector/ODBC
          OdbcConnection MyConnection = new OdbcConnection(MyConString);
          MyConnection.Open();
 ...

Using the MySql.Data.MySqlClient.MySqlConnection class, MySQL connection should be of this form:

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

(Default port is 3306)

If you are using the MySQL Connector/ODBC 3.51 (which your code suggests):

Driver={MySQL ODBC 3.51 Driver};
Server=myServerAddress;Database=myDataBase;
User=myUsername; Password=myPassword;Option=3;

The ConnectionStrings site is a great resource.

Mitch Wheat
thanks for the informaiton let me try this.
sfgroups
MySQL Connector Net 6.3.5\Documentation comes with documentation, this file has all the sameple code : MySql.Data.chm
sfgroups