Connecting to MySQl database

This is a discussion on Connecting to MySQl database within the C# Programming forums, part of the General Programming Boards category; Hi, Trying to use c# to connect to a mysql database that I have set up on a server somewhere, ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    Ballincollig, co. cork, Eire
    Posts
    22

    Connecting to MySQl database

    Hi,

    Trying to use c# to connect to a mysql database that I have set up on a server somewhere, but I am currently getting the following error:

    Error 11 The type 'System.Data.Common.DbConnection' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. C:\Documents and Settings\mb21\My Documents\Visual Studio 2008\Projects\Database3\Database3\Form1.cs 17 25 Database3
    The code I am using is the following:

    Code:
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using MySql.Data;
    using MySql.Data.MySqlClient;
    
    namespace Database3
    {
        public partial class Form1 : Form
        {
            MySqlConnection conn = null;
            MySqlDataReader dr;
            MySqlCommand cmd = null;
    
            public Form1()
            {
                InitializeComponent();
            }
    
           
    
    
            private void DisplayErrors(MySqlException errSQLException)
            {
                for (int i = 0; i < errSQLException.Errors.Count; i++)
                {
                    MessageBox.Show("Index # " + i + "\n" + "Error:" +
                        errSQLException.Errors[i].ToString() + "\n");
                }
            }
    
            private void btnConnect_Click_1(object sender, EventArgs e)
            {
                string strSQL = "Select * from users";
               
                try
                {
                    cmd = new MySqlCommand(strSQL, conn);
                    dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
    
                    while (dr.Read())
                    {
    
                        txtData.Text = dr.GetString(1);
                        
                    }
                }
                catch (MySqlException ex)
                {
                    DisplayErrors(ex);
                }
                finally
                {
                    dr.Close();
                    conn.Close();
                }
            }
    
            private void Form1_Load_1(object sender, EventArgs e)
            {
                string strConnection = "SERVER=server;" + "DATABASE=database;" + "UID=username;" + "PASSWORD=password;";
                                //"Data Source = .\\SQLEXPRESS;" +
                                //"Initial Catalog=SampleDatabase.mdf;";// +
                               //"Integrated Security=SSPI;" +
                              // "User ID=mb21;" +
                               //"Password=mscim2010;";
                
            
                txtData.Text = "blah";//This line does run
                try
                {
                    conn = new MySqlConnection(strConnection);
                    txtData.Text = txtData.Text + "anything";//This line does run
                    conn.Open();
                  
                }
                catch (MySqlException ex)
                {
                    DisplayErrors(ex);
                    conn.Close();
                }
            }      
        }
    }
    There are several errors, all like the one on the top, which correspond to the following lines:
    Code:
    MySqlConnection conn = null;
    MySqlDataReader dr;
    MySqlCommand cmd = null;
    
    private void DisplayErrors(MySqlException errSQLException)
    I installed mysql connector.net version 6.1.3 and I am using a Visual Studio 2008 for developing the code.

    I am new to C#, so would be grateful for any help. It seems the problem is lies with the declaration of the variables (conn, dr, cmd and errSQLException) but I can't see what the problem is. Any and all help is appreciated.

    Brownie

  2. #2
    Registered User
    Join Date
    Feb 2006
    Location
    Ballincollig, co. cork, Eire
    Posts
    22
    Also trying to connection to a local mssql server, but not having any luck with that. Just if anyone is wondering why this

    //"Data Source = .\\SQLEXPRESS;" +
    //"Initial Catalog=SampleDatabase.mdf;";// +
    //"Integrated Security=SSPI;" +
    // "User ID=mb21;" +
    //"Password=mscim2010;";
    is commented out.

    The end result of the code will be deployed on the Windows Mobile platform (if that helps in any way)
    Brownie

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    First of all, connection strings can be difficult sometimes to get working. Go here: ConnectionStrings.com - Forgot that connection string? Get it here! for some help on that. Second, What are the errors you're getting? Are you getting errors like "The type of namespace name 'MySql' could not be found (are you missing a using directive or an assembly reference?)"???? If not, what are they?
    The keyboard is the standard device used to cause computer errors!

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    I was looking into MySql connector.net. Are you sure you have a reference added to your project? Im guessing your getting the errors i talked about above and thats because there is no reference to using MySql.Data and MySql.Data.MySqlClient. So your program isn't aware of the MySqlConnection or the MySqlDataReader types. You need to make sure your program has a reference to this MySql connector.
    The keyboard is the standard device used to cause computer errors!

  5. #5
    Registered User
    Join Date
    Feb 2006
    Location
    Ballincollig, co. cork, Eire
    Posts
    22
    Hey stumon,

    Thanks for replying. The errors I am getting are all like the following:

    The type 'System.Data.XXXXXXXXXX' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.......'
    I don't know why it is using System.Data when they all should be MySql.Data. I do have a reference added for the MySql.Data that was downloaded with Connector.net, but maybe adding one for MySql.Data.MySqlClient might just do the trick.

    Thanks again.
    Brownie

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Code:
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient
    Is there a System.Data or two in there or what?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Connecting to a mySQL database using C#
    By Mavix in forum C# Programming
    Replies: 2
    Last Post: 11-26-2007, 05:19 AM
  2. Error connecting from Dev C++ to MS Access Database
    By Sridar in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2006, 06:09 PM
  3. Problem connecting to MySQL Database?
    By MrLucky in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2006, 10:30 AM
  4. About C++ and MySQL or oether free database
    By xxxrugby in forum C++ Programming
    Replies: 18
    Last Post: 12-30-2005, 05:28 AM
  5. Connecting to MySQL Database
    By ussj4gohan in forum C++ Programming
    Replies: 4
    Last Post: 10-13-2001, 09:41 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21