0

I have been trying to connect to a MySQL database but have been getting exception 0x80131904: The server was not found. I know the server is running and I can query it through the command line. I have also made sure skip networking is not enabled. What am I doing wrong?

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
4

3 回答 3

2

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

为了能够从 C# 连接到 MySQL,您需要MySQL Connector/Net。它带有一个MySqlConnection类和相应的数据读取器和参数类。

介绍可以在这里找到。

于 2013-04-22T05:25:54.713 回答
0
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);
}
于 2013-04-22T05:29:43.813 回答
0

尝试使用 127.0.0.1 而不是 'localhost' 作为您的服务器。

于 2013-04-22T05:19:26.433 回答