首页 > 解决方案 > 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();
            }
        }
    }

标签: c#mysql

解决方案


我认为您的连接字符串有问题。尝试使用 MySqlConnectionStringBuilder:

MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
builder.Host = "localhost";
builder.UserId = "root";
builder.Database = "escola";
builder.Password = "usbw";
connection = new MySqlConnection(builder.ConnectionString);

推荐阅读