none
How to avoid doing this to connect to mysql from registry info RRS feed

  • Question

  • Hi, I am trying to connect to mysql database from info my program have in registry, I can successful do this, but I am affraid I am not doing it in the best way, here is my code, it is working, but I want to optimize it:

    Code Block

    public class AccionesSQL
        {
            public static DataTable DatosRegistro()
            {
                DataTable TablaDatos = new DataTable();
                TablaDatos.Columns.Add("Server");
                TablaDatos.Columns.Add("DB");
                TablaDatos.Columns.Add("User");
                TablaDatos.Columns.Add("Password");
                object[] row = new object[4];
                RegistryKey direccion_registro = Registry.LocalMachine.CreateSubKey("SOFTWARE\\MyProgram");
                if (direccion_registro == null)
                {
                    Console.WriteLine("HKEY_LOCAL_MACHINE\\SOFTWARE\\MyProgram doesn't exist");
                }
                else
                {
                   
                    row[0] = direccion_registro.GetValue("Server").ToString();
                    row[1] = direccion_registro.GetValue("DB").ToString();
                    row[2] = direccion_registro.GetValue("User").ToString();
                    row[3] = direccion_registro.GetValue("Password").ToString();
                    TablaDatos.Rows.Add(row);
                }
                direccion_registro.Close();
                return TablaDatos;
            }

    public static DataTable Reg = AccionesSQL.DatosRegistro(); //Stupid step ?

            public static MySqlConnection conexion = new MySqlConnection(string.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false;", AccionesSQL.Reg.Rows[0][0].ToString(), AccionesSQL.Reg.Rows[0][2].ToString(), AccionesSQL.Reg.Rows[0][3].ToString(), AccionesSQL.Reg.Rows[0][1].ToString())); //Cant I directly fill from AccionesSQL.DatosRegistro ?

    public static DataTable Load_Info(string query)
            {
                MySqlDataAdapter dataAdapter = new MySqlDataAdapter(query, AccionesSQL.conexion);
                MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(dataAdapter);
                DataTable DatosTabla = new DataTable();
                DatosTabla.Locale = System.Globalization.CultureInfo.InvariantCulture;
                dataAdapter.Fill(DatosTabla);
                return DatosTabla;
            }

    //Now, If I want to search info in database I just use:

    (In a form)

    ...
    DataTable StuffFromDb = AccionesSQL.Load_Info("SELECT * FROM stuff");




    Friday, December 7, 2007 11:53 PM

Answers

  • Hi Gohalien,

     

    As far as I know, there are various ways to connect to MySQL database, but you need to install the corresponding drivers in your PC correctly before using them.

    1) Connect to MySQL via OLE DB provider and you can try to check out this article about "Opening MySQL database with C#"  -  http://www.csharphelp.com/archives2/archive288.html

    Here are some sample codes to connect to MySQL:

    Code Block
       System.Data.OleDb.OleDbConnection con;
       con = new System.Data.OleDb.OleDbConnection("");
       con.ConnectionString="Provider=MySQLProv;Data Source=mysql;";
       try
       {
        con.Open();
        if(con.State==ConnectionState.Open) MessageBox.Show("Connection to MySQL opened through OLE DB Provider");
        con.Close();
       }
       catch(Exception ex)
       {
         MessageBox.Show(ex.Message);
       }

     

    2) Connect via MySQL connector and you can try to check out this article about "How to connect to MySQL 5.0. via C# .Net and the MySql Connector/Net" for reference -  http://bitdaddys.com/MySQL-ConnectorNet.html

    Here are some sample codes to connect to MySQL:

    Code Block

    string MyConString = "SERVER=localhost;DATABASE=mydatabase;UID=testuser;PASSWORD=testpassword;";

     

    MySqlConnection connection = new MySqlConnection(MyConString);

     

    For your concerned sample codes as follows:

    Code Block

    public static DataTable Reg = AccionesSQL.DatosRegistro(); //Stupid step ?

     

    public static MySqlConnection conexion = new MySqlConnection(string.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false;", AccionesSQL.Reg.Rows[0][0].ToString(), AccionesSQL.Reg.Rows[0][2].ToString(), AccionesSQL.Reg.Rows[0][3].ToString(), AccionesSQL.Reg.Rows[0][1].ToString())); //Cant I directly fill from AccionesSQL.DatosRegistro ?

    As I understand, I think you can fill MySQL connection string from AccionesSQL.DatosRegistro directly. It is not neccessary to fill it through a temp Reg variable via this code line - "public static DataTable Reg = AccionesSQL.DatosRegistro(); " You can connect to MySQL database in the method named "DatosRegistro" as follows:

    Code Block

    public static MySqlConnection conexion = new MySqlConnection(string.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false;", direccion_registro.GetValue("Server").ToString(), direccion_registro.GetValue("DB").ToString(), direccion_registro.GetValue("User").ToString(), direccion_registro.GetValue("Password").ToString());

     

    Hope this helps,

    Regards,

     

    This response contains links reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you.
    Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.
    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Monday, December 10, 2007 5:21 AM