C#未连接到数据库(SQL)

时间:2018-11-07 12:36:58

标签: c# mysql

我有一个用于管理学校(学生和班级)的数据库。 我有一个带有代码的类,用于连接数据库,然后在主程序中调用函数。 当我尝试与数据库进行交互时,它警告我它无法连接至数据库或超出了连接时间。 我试图添加一个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();
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:-1)

尝试一下:

   connection_string  = @"Data Source = " + server  + "; Initial Catalog = " + data_base  + "; Integrated Security=True;uid=myUser;password=myPass;";