How do I connect to a database and loop over a recordset in C#?

前端 未结 8 2148
孤独总比滥情好
孤独总比滥情好 2021-01-03 17:44

What\'s the simplest way to connect and query a database for a set of records in C#?

8条回答
  •  说谎
    说谎 (楼主)
    2021-01-03 18:11

    Charge the libraries

    using MySql.Data.MySqlClient;
    

    This is the connection:

    public static MySqlConnection obtenerconexion()
    {
        string server = "Server";
        string database = "Name_Database";
        string Uid = "User";
        string pwd = "Password";
        MySqlConnection conect = new MySqlConnection("server = " + server + ";" + "database =" + database + ";" + "Uid =" + Uid + ";" + "pwd=" + pwd + ";");
    
        try
        {
            conect.Open();
            return conect;
        }
        catch (Exception)
        {
            MessageBox.Show("Error. Ask the administrator", "An error has occurred while trying to connect to the system", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return conect;
        }
    }
    

提交回复
热议问题