C# Can';t连接到mysql 5.6数据库

C# Can';t连接到mysql 5.6数据库,c#,mysql,localhost,database-connection,C#,Mysql,Localhost,Database Connection,我一直试图连接到MySQL数据库,但出现异常0x80131904:找不到服务器。我知道服务器正在运行,我可以通过命令行查询它。我还确保没有启用跳过网络。我做错了什么 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Data.SqlClient

我一直试图连接到MySQL数据库,但出现异常0x80131904:找不到服务器。我知道服务器正在运行,我可以通过命令行查询它。我还确保没有启用跳过网络。我做错了什么

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Data.SqlClient;

class Program
    {
    static void Main(string[] args)
        {

        Console.WriteLine("Connecting to database...");

        SqlConnection sqlserver = new SqlConnection("user id=<removed>;" +
                                  "password=<removed>;server=localhost;" +
                                  "Trusted_Connection=yes;" +
                                  "database=ircbot; " +
                                  "connection timeout=5");

        try
            {
            sqlserver.Open();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Connected");
            Console.ResetColor();
            }
        catch (Exception e)
            {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(e.ToString());
            Console.ResetColor();
            }

        Console.ReadLine();
        }    //end main


    }  //end class Program
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
使用System.Data.SqlClient;
班级计划
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“连接到数据库…”);
SqlConnection sqlserver=newsqlconnection(“用户id=;”+
“密码=;服务器=本地主机;”+
“受信任的连接=是;”+
“数据库=ircbot;”+
“连接超时=5”);
尝试
{
sqlserver.Open();
Console.ForegroundColor=ConsoleColor.Green;
控制台。写入线(“连接”);
Console.ResetColor();
}
捕获(例外e)
{
Console.ForegroundColor=ConsoleColor.Red;
Console.WriteLine(如ToString());
Console.ResetColor();
}
Console.ReadLine();
}//末端总管
}//结束类程序

尝试使用127.0.0.1而不是“localhost”作为服务器。

SqlConnection
仅用于连接到Microsoft SQL server,不能使用它连接到任何其他DBMS,如MySQL

要从C#连接到MySQL,您需要。它附带了一个
MySqlConnection
类以及相应的数据读取器和参数类


可以找到一个介绍。

您正在尝试连接到sql server还是mysql?我正在尝试连接到mysql
SqlConnection
仅用于连接到Microsoft sql server。你不能用它连接到MySQL。要从C#连接到MySQL,请看。你认为这有什么不同?当我编写连接到数据库的php脚本时,当我使用localhost时它不起作用,但当我输入127.0.0.1时它起作用。不知道为什么,但事情就是这样。@MichaelDeluca:谢谢你的澄清。我只是想确定你的答案是基于以下事实:)
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;

myConnectionString = "server=127.0.0.1;uid=root;" +
"pwd=12345;database=test;";

try
{
conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = myConnectionString;
conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
    MessageBox.Show(ex.Message);
}