how to connect c# to a localhost mysql database?

snfmanutz

New member
Joined
Jun 4, 2021
Messages
2
Programming Experience
10+
, i have used vs & mysql but now i'm lost.. I have looked online but the examples dont work on my machine (windows 10 running VS2019.

I have placed the using statements
C#:
using MySql.Data;
using MySql.Data.MySqlClient;

in my code but that doesnt resolve ..... OdbcCommand command = new OdbcCommand(queryString);

help below is a piece of code i copied from the internet. the compiler gives me errors on the using statement.
i added the commenting-out .
C#:
/* ======================================================
using System;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace localdb
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            InsertRow(@"localhost/mydb");
        }

        static void InsertRow(string connectionString)
        {
            string queryString =
                "INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')";

            OdbcCommand command = new OdbcCommand(queryString);

            using (OdbcConnection connection = new OdbcConnection(connectionString))
            {
                command.Connection = connection;
                connection.Open();
                command.ExecuteNonQuery();

                // The connection is automatically closed at
                // the end of the Using block.
            }
        }
    }
}
======================================================
 */
 
Last edited by a moderator:
And you are who?

If you have 10 years experience, then you wouldn't be looking up jazz on the net, because most of your knowledge will be gained through wisdom and your own experience.

Do I learn from other people? No not really.

I do make mistakes sometimes, and if someone is kind enough to correct me, then I learn from that. (Its a rarity.) But I don't scour the net looking for code to build my apps. I also don't use the net to troubleshoot bad code I've written. Writing good code comes with experience. That is also what documentation is for. And If I was using MySQL in a project, then I would be using the correct DLL's and connectors for that project. (A frequenter of MySQL, I am also a MySQL administrator.). Writing a using statement does not give you "magic" access to the library who's using directive you typed. It will only let you use that using directive providing you've loaded in the assemblies for the referenced using directive.

I would also not be mixing up OdbcCommand for MySqlCommand's and the same applies to connections and permeameters or any other object specific to the referenced libs.
 
And you are who?

If you have 10 years experience, then you wouldn't be looking up jazz on the net, because most of your knowledge will be gained through wisdom and your own experience.

Do I learn from other people? No not really.

I do make mistakes sometimes, and if someone is kind enough to correct me, then I learn from that. (Its a rarity.) But I don't scour the net looking for code to build my apps. I also don't use the net to troubleshoot bad code I've written. Writing good code comes with experience. That is also what documentation is for. And If I was using MySQL in a project, then I would be using the correct DLL's and connectors for that project. (A frequenter of MySQL, I am also a MySQL administrator.). Writing a using statement does not give you "magic" access to the library who's using directive you typed. It will only let you use that using directive providing you've loaded in the assemblies for the referenced using directive.

I would also not be mixing up OdbcCommand for MySqlCommand's and the same applies to connections and permeameters or any other object specific to the referenced libs.

Here is what I needed to do ...
add these three lines to my "program.csproj" file ...

<ItemGroup>
<PackageReference Include="MySql.Data" Version="8.0.25" />
</ItemGroup>

Now it compiles and the errors i get are my own.
How did i find this ...
via the internet of course!

Thanks for your time.
i appreciate your help.

jim murray
programming for 60+ years and still learning
 
OP - You can not just add a using statement to your file and expect it to work. You need to add a reference to the library you are using. Add references in the Reference Manager - Visual Studio
I did tell you that already. Frankly, if you are programming that long, I'd expect you to know how to add references through the interface or manually.

In any respect. at least you got some of it working. I suggest you brush up on the MySQL documentation for C#.
 
Last edited:
To connect C# with MySQL, you can use the MySQL Connector/NET library, which provides a convenient way to interact with MySQL databases. First, ensure you have the MySQL Connector/NET installed in your project. Then, import the necessary namespaces (MySql.Data.MySqlClient) in your C# code. Create a MySqlConnection object, specifying the connection string, which includes details like the server, database name, username, and password. Open the connection using Open() method, execute SQL commands using MySqlCommand, and retrieve data using MySqlDataReader. After performing database operations, close the connection with Close(). Remember to handle exceptions and dispose of resources properly to ensure robust and secure database connectivity.
 
If you have 10 years experience, then you wouldn't be looking up jazz on the net

I must be really thick then, because I've been coding for 20 years and I look stuff up on the net every day :D

To connect C# with MySQL, you can use the MySQL Connector/NET library

More than 2 years later, they probably solved the query by now
 
Back
Top Bottom