c# - C# 不连接到数据库 (SQL)
问题描述
我有一个数据库来管理学校(学生和班级)。我有一个带有连接数据库的代码的类,然后我调用主程序中的函数。当我尝试与数据库交互时,它警告我它无法连接到数据库或超过了连接时间。我试图添加一个 ssslmode,但它没有用。我也尝试添加一个端口,但它没有用。
类的代码:
public class ligacao
{
public MySqlConnection connection;
string server;
public string data_base;
string user_id;
string password;
public void inicializa()
{
server = "localhost";
data_base = "escola";
user_id = "root";
password = "usbw";
string connection_string;
string sslmode = "none";
connection_string = "SERVER=" + server + ";" + "DATABASE=" + data_base + ";" + "UID=" + user_id + "PASSWORD=" + password + ";" + "SslMode=" + sslmode + ";";
connection = new MySqlConnection(connection_string);
}
public bool open_connection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0: MessageBox.Show("Couldn't connect t DataBase."); break; // couldn't connect to database
case 1042: MessageBox.Show("Exceded the connection time"); break; // exceeded the connection time
case 1045: MessageBox.Show("Username/password are incorrect"); break;
}
return false;
}
}
public bool close_connection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}
主程序代码:
public partial class consultas : Form
{
ligacao x = new ligacao();
public consultas()
{
InitializeComponent();
x.inicializa();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void consultas_Load(object sender, EventArgs e)
{
//define query
string query = "SELECT designacao FROM disciplinas";
//open connection
if (x.open_connection())
{
//create the comand and associates the query with the connection through the connector
MySqlCommand cmd = new MySqlCommand(query, x.connection);
//create datareader and execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//show data in combobox1
if (dataReader.Read())
{
comboBox1.Items.Add(dataReader["designacao"]);
}
//close dataReader
dataReader.Close();
//close connection
x.close_connection();
}
//define query
string queryBI = "SELECT bi FROM alunos";
//open connection
if (x.open_connection())
{
//create the commando and associate the query with the connection through the constructor
MySqlCommand cmd = new MySqlCommand(queryBI, x.connection);
//create datareader and execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//show data in combobox1
if (dataReader.Read())
{
comboBox1.Items.Add(dataReader["bi"]);
}
//close dataReader
dataReader.Close();
//close connection
x.close_connection();
}
}
}
解决方案
我认为您的连接字符串有问题。尝试使用 MySqlConnectionStringBuilder:
MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
builder.Host = "localhost";
builder.UserId = "root";
builder.Database = "escola";
builder.Password = "usbw";
connection = new MySqlConnection(builder.ConnectionString);
推荐阅读
- reactjs - 在从表单中获取的值上生成唯一 ID 的最简单方法(React)
- c# - Outlook 插件-缩小的功能区组图标
- elasticsearch - ElasticSearch:[multi_match] 查询不支持 [search_analyzer]
- mapbox - 从 Mapbox Tilequery API 下载数据
- ngrx - ngrx 实体适配器未更新所需属性的子属性
- python - 带有通配符的 Python LIKE 中的 MySql 查询
- javascript - 使用 javascript 删除包含标签和文本框的动态添加的表单组
- html - Bootstrap 4 .card 溢出时受父高度限制
- r - 分组并保留具有匹配模式的列
- flutter - 如何使用 Flutter 和 Dart 在后台监听 BLE 信号?