我想連接MySql數據庫到我的窗體。我得到了連接字符串,我右鍵單擊我的數據庫連接並轉到屬性。連接字符串連接到MySql數據庫?
connection.ConnectionString = "server=localhost;user id=root;database=island_dealership;"+
"persistsecurityinfo=True;allowuservariables=True";
下面是錯誤和完整代碼的圖像。
car.cs類:在這裏我試圖連接到數據庫。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace DatabaseProject
{
public partial class Car : Form
{
MySqlConnection connection = new MySqlConnection();
MySqlConnection cmd;
public Car()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
connection.ConnectionString = "server=localhost;user id=root;database=island_dealership;persistsecurityinfo=True;allowuservariables=True";//Connection String....
connection.Open();
MessageBox.Show("Connected to MySql database");
}
catch(Exception e1)
{
MessageBox.Show("Connection failed Due to " + e1.ToString());
}
}
private void submitCar_Click(object sender, EventArgs e)
{
}
}
}
你不指定密碼。 [http://www.connectionstrings.com/mysql/](http://www.connectionstrings.com/mysql/) – MickyD
連接字符串缺少密碼。你必須添加密碼。它應該是這樣的一些東西:「數據源=服務器名稱\實例名稱;初始目錄=數據庫名稱;用戶名= SQLLoginName;密碼= SQLLoginPassword; –
不要使用'root'用戶 –