[英]Can't connect to mysql 5.6 database
我一直在嘗試連接到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
SqlConnection
僅用於連接到Microsoft SQL Server,您不能使用它連接到任何其他DBMS,如MySQL。
為了能夠從C#連接到MySQL,您需要MySQL Connector / Net 。 它附帶了一個MySqlConnection
類和相應的數據讀取器和參數類。
可在此處找到介紹。
嘗試使用127.0.0.1而不是'localhost'作為您的服務器。
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.