can anyone help me, I am trying to connect mysql database, the server details are entered correctly, but when I call the open method, the program throws an exception.
I also used Server explorer, in this case the program sees the server, but I don’t know how to use the database connected through Server explorer, that is, how to add it to the project itself.
Please tell me how to fix this error, or how to use the database through Server explorer.
This is how I connect to the database:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BDtest
{
class Program
{
static void Main(string[] args)
{
SqlConnection sqlConnection = new SqlConnection("server=localhost;user=root;database=clothing_shop;password=****;");
sqlConnection.Open();
string queri = "select world.countrylanguage.CountryCode from world.countrylanguage where CountryCode = 'ARM'";
SqlCommand cmd = new SqlCommand(queri, sqlConnection);
var value = cmd.ExecuteScalar().ToString();
Console.WriteLine(value);
}
}
}
2
Answers
Looking at your code, you are using
System.Data.SqlClient
which is a .NET Data Provider for SQL Server.In your question you state that you try to connect to a mysql database. Use the appropriate nuget package for that
You can’t connect using SqlConnection. you need to install MySqlConnection using nuget package. You can download from https://www.nuget.org/packages/MySqlConnector and you can look how to use section from readme.