MySql doesn't work in Visual Studio 2012 : The type or namespace name 'MySql' could not be found

37,370

Solution 1

You need to add reference to MySql.Data.dll but it is better you can add this dll as package by using NuGet

below is the Package Manager Console command

PM> Install-Package MySql.Data

Solution 2

Add a reference from the project to the MySql.Data.dll as well.

using doesn't make assemblies available. It only allows you to use entries from a namespace without specifying that namespace each time.

Solution 3

You must add it as a reference

enter image description here

Solution 4

We'll be adding support for VS 2012 in Connector/NET 6.5.5 and later 6.6.x version http://forums.mysql.com/read.php?38,546265,564533#msg-564533

and give a link to test a trick

http://social.technet.microsoft.com/wiki/pt-br/contents/articles/10476.instalando-mysql-connector-no-visual-studio-2011-beta.aspx

Refer this answer

Solution 5

Just an idea,

Looks like you're missing MySql.Data in your project which you can declare in your web.config

Make sure you have your web.config properly configured.

 <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
 <assemblyIdentity name="MySql.Web" publicKeyToken="c5687fc88969c44d" culture="neutral" />
Share:
37,370
JAN
Author by

JAN

The biggest C# and JAVA enthusiastic ever existed.

Updated on July 05, 2022

Comments

  • JAN
    JAN almost 2 years

    Given this code :

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    //Include mysql client namespace.
    
    using MySql.Data.MySqlClient;   // That one doesn't work !!!
    using System.Configuration;
    
    namespace CSharpMySqlSample
    {
        public partial class frmMySqlSample : Form
        {
            //Read connection string from application settings file
            string   ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
            MySqlConnection connection;
            MySqlDataAdapter adapter;
            DataTable DTItems;
            public frmMySqlSample()
            {
                InitializeComponent();
            }
    
            private void frmMySqlSample_Load(object sender, EventArgs e)
            {
                //Initialize mysql connection
                connection = new MySqlConnection(ConnectionString);
    
                //Get all items in datatable
                DTItems = GetAllItems();
    
                //Fill grid with items
                dataGridView1.DataSource = DTItems;
            }
    
            //Get all items from database into datatable
            DataTable GetAllItems()
            {
                try
                {
                    //prepare query to get all records from items table
                    string query = "select * from items";
                    //prepare adapter to run query
                    adapter = new MySqlDataAdapter(query, connection);
                    DataSet DS = new DataSet();
                    //get query results in dataset
                    adapter.Fill(DS);
    
                    // Set the UPDATE command and parameters.
                    adapter.UpdateCommand = new MySqlCommand(
                        "UPDATE items SET ItemName=@ItemName, Price=@Price, AvailableQuantity=@AvailableQuantity, Updated_Dt=NOW() WHERE ItemNumber=@ItemNumber;",
                        connection);
                    adapter.UpdateCommand.Parameters.Add("@ItemNumber", MySqlDbType.Int16, 4, "ItemNumber");
                    adapter.UpdateCommand.Parameters.Add("@ItemName", MySqlDbType.VarChar, 100, "ItemName");
                    adapter.UpdateCommand.Parameters.Add("@Price", MySqlDbType.Decimal, 10, "Price");
                    adapter.UpdateCommand.Parameters.Add("@AvailableQuantity", MySqlDbType.Int16, 11, "AvailableQuantity");
                    adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
    
                    // Set the INSERT command and parameter.
                    adapter.InsertCommand = new MySqlCommand(
                        "INSERT INTO items VALUES (@ItemNumber,@ItemName,@Price,@AvailableQuantity,NOW());",
                        connection);
                    adapter.InsertCommand.Parameters.Add("@ItemNumber", MySqlDbType.Int16, 4, "ItemNumber");
                    adapter.InsertCommand.Parameters.Add("@ItemName", MySqlDbType.VarChar, 100, "ItemName");
                    adapter.InsertCommand.Parameters.Add("@Price", MySqlDbType.Decimal, 10, "Price");
                    adapter.InsertCommand.Parameters.Add("@AvailableQuantity", MySqlDbType.Int16, 11, "AvailableQuantity");
                    adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
    
                    // Set the DELETE command and parameter.
                    adapter.DeleteCommand = new MySqlCommand(
                        "DELETE FROM items "
                        + "WHERE ItemNumber=@ItemNumber;", connection);
                    adapter.DeleteCommand.Parameters.Add("@ItemNumber",
                      MySqlDbType.Int16, 4, "ItemNumber");
                    adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None;
    
                    //return datatable with all records
                    return DS.Tables[0];
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                return null;
            }
    
            private void btnSave_Click(object sender, EventArgs e)
            {
                try
                {
                    //Save records in database using DTItems which is datasource for Grid
                    adapter.Update(DTItems);
                    //Refresh grid
                    DTItems = GetAllItems();
                    dataGridView1.DataSource = DTItems;
                    MessageBox.Show("Items saved successfully...");
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
            private void btnDelete_Click(object sender, EventArgs e)
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {
                    //Delete a row from grid first.
                    dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
    
                    //Save records again. This will delete record from database.
                    adapter.Update(DTItems);
    
                    //Refresh grid. Get items again from database and show it in grid.
                    DTItems = GetAllItems();
                    dataGridView1.DataSource = DTItems;
                    MessageBox.Show("Selected item deleted successfully...");
                }
                else
                {
                    MessageBox.Show("You must select entire row in order to delete it.");
                }
            }
        }
    }
    

    After compilation I get :

    error CS0246: The type or namespace name 'MySqlConnection' could not be found (are you missing a using directive or an assembly reference?)
    

    But I checked the connector : enter image description here

    And it's installed ..

    So what's wrong ?

    Thanks

  • Angel M.
    Angel M. about 8 years
    Thanks a lot, this is the only thing that work on my Ubuntu
  • Charitha Goonewardena
    Charitha Goonewardena about 7 years
    after installing this add this one from browse which is located under the add reference panel
  • AStopher
    AStopher over 6 years
    @CharithaGoonewardena Shouldn't need to do that.
  • AStopher
    AStopher over 6 years
    Note that this isn't a web project so there will be no web.config file, but good advice for future reference.