c#未连接到数据库(sql)

zyfwsgd6  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(339)

我有一个数据库来管理学校(学生和班级)。我有一个类,其中包含连接到数据库的代码,然后调用主程序中的函数。当我尝试与数据库交互时,它会警告我它无法连接到数据库或已超过连接时间。我试图添加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();
            }
        }
    }
hmmo2u0o

hmmo2u0o1#

试试这个:

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

gk7wooem2#

我想你的连接字符串有问题。尝试使用mysqlconnectionstringbuilder:

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

相关问题