Results 1 to 6 of 6

Thread: Connecting C# with MySQL database

  1. #1
    Join Date
    May 2008
    Posts
    255

    Connecting C# with MySQL database

    I need some explanation and related matter regrading application development.I decided to put the MySQL database as a back end for my C# developed application.How would I get success to make the connection between C# and MySQL database.

    If you have any idea and related stuff then please provide me as soon as possible .

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Connecting C# with MySQL database

    There are different ways to make connection with MySQL database from a .NET application.The developers waste own time to connecting a .NET application to a MySQL server.

    One of the easiest way to make connection with the MySQL database,the name of the driver which is used in the connection MyODBC driver which perform the same functionality as the ODBC driver.

    MyODBC is very simplest, cleanest driver to work with .NET applications which works like a charm.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Setting up the MyODBC driver

    Before doing anything,you need to download and install the MyODBC driver from the official site of MySQL database.

    After downloading,install it on your machine where application is running.Now start the main wizard of for connection.

    1- Go to control panel and open Data Sources (ODBC).
    2- Click on Add button,a connection wizard would be open .
    3- Fill the required information regrading MySQL server,user name and password.
    4- Now,click on TEST button to make sure about the connection

    The wizard will occur a message box with successful connection if your information is given absolute.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Connecting C# with MySQL database

    After saving the connection,you are not having a direct access to the the database.there are some other task is needed to complete the successful connection.

    After clicking OK to save the information, you will be prompted with list of created Data Source .The connection name and driver would be shown in the list.If you need to configure your connection again then you can do it using the configure button in the right side of the wizard.

    Now,you need to include your project on which you want to create a connection.This was the section task in which we configured the driver for the connection.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Configuring the C#codes for connection

    Now you require to commit some changes at application level and need to edit the code in this form -

    Code:
    private  System.Data.Odbc.OdbcConnection OdbcCon;
    private System.Data.Odbc.OdbcCommand OdbcCom;
    private System.Data.Odbc.OdbcDataReader OdbcDR;
    private string ConStr
    ;

    where ODBC connection,command and data reader will play the most important role to make connection ,query and read data from the MySQL Server.One more variable ConStr is also declared to holds the connection string.

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Editing the Connect button code

    Editing the Connect button code

    I am going to show you the code which would be activated when the connect button clicked.The code on click event is as follows-

    Code:
    private void btnConnect_Click(object sender, EventArgs e)
    
    {
    ConStr = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=" + txtIP.Text + ";PORT=" + txtPort.Text + ";DATABASE=" + txtDatabase.Text + ";UID=" + txtUsername.Text + ";PWD=" + txtPassword.Text + ";OPTION=3";
    
    OdbcCon = new System.Data.Odbc.OdbcConnection(ConStr);
    
    btnListTables.Enabled = true;
    
    try
    {
    txtLog.AppendText("Openning connection...\r\n"); 
    
    if  (OdbcCon.State == ConnectionState.Closed)
    
    {
    OdbcCon.Open(); 
    txtLog.AppendText("Connection opened\r\n"); 
    }
    
    }
    catch  (System.Data.Odbc.OdbcException Ex) 
    {
    txtLog.AppendText(Ex.Message + "\r\n"); 
    }
    }

Similar Threads

  1. How to connect PHP to mySQL Database
    By Nathen in forum Tips & Tweaks
    Replies: 2
    Last Post: 17-07-2011, 03:49 AM
  2. mysqldump in MySQL database
    By Caden Fernandes in forum Software Development
    Replies: 4
    Last Post: 04-03-2010, 11:02 PM
  3. Issues in C# application connecting to Remote MySQL 5 database.
    By kyosang in forum Software Development
    Replies: 5
    Last Post: 25-01-2010, 04:24 PM
  4. Replies: 3
    Last Post: 07-11-2009, 09:36 PM
  5. How to Connect MySQL database from PHP
    By Booth in forum Software Development
    Replies: 3
    Last Post: 21-01-2009, 09:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,113,420.21833 seconds with 16 queries