簡體   English   中英

使用 C# ( Visual Studio ) 連接 MySQL 服務器失敗

[英]Failure connecting MySQL server with C# ( Visual Studio )

使事情簡短明了:

我正在嘗試將我的 Windows 窗體應用程序連接到 mySQL 服務器。 我已經按照網絡上的所有說明進行操作,但沒有成功。

我已經按照本文中的所有步驟操作,但沒有成功: http : //net-informations.com/q/faq/mysql.html

無論我嘗試什么,它都不會連接。

我已經安裝: - mysql 連接器

我還在頂部添加了 mysql.data 引用。

奇怪的是,連接到本地 sql server 不是問題。 它將連接而不會出現任何故障。

這是我正在運行的代碼(本地 SQL):

 using System; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace csharp_mysql { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { MySqlConnection myConn = new MySqlConnection("Server=localhost;Database=users;Uid=root;Pwd=plopPlop1234;"); myConn.Open(); MySqlDataAdapter myDataAdapter = new MySqlDataAdapter(); MySqlCommand selectCommand = new MySqlCommand("select * from users.users where username='" +textBox1.Text+ "' and password='" +textBox2.Text+ "'", myConn); MySqlDataReader myReader; myReader = selectCommand.ExecuteReader(); MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter); int count = 0; while (myReader.Read()) { count += 1; } if (count == 1) { MessageBox.Show("Login successful"); Form2 f2 = new Form2(); f2.Show(); this.Hide(); } else if(count > 0) { MessageBox.Show("duplicate username and password"); } else { MessageBox.Show("username or password not found"); } textBox1.Clear(); textBox2.Clear(); } catch(Exception ex) { MessageBox.Show("There is no connection to be made); } }

一旦我將憑據更改為另一個真正的 mysql 服務器(我自己的主機),它將停止連接。

這里有什么問題,我在這里錯過了什么?

如果您的錯誤是“無法連接到任何指定的 MySQL 主機。”,請檢查您的主機是否允許遠程連接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM