0

What I want to do is have text boxes for the user to input the required fields for the connection to the MySQL table, Here is the code I currently have.

    public partial class Form1 : Form
        {
           MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
           conn_string.Server = serverTextBox.Text;
           conn_string.UserID = userTextBox.Text;
           conn_string.Password = passwordtextBox.Text;
           conn_string.Database = dataBaseTextBox.Text;

           using (MySqlConnection mcon = new MySqlConnection(conn_String.ToString()));
           MySqlCommand mcd;
           MySqlDataAdapter mda;


            //-----open connection-----//
            public void openCon()
            {
                if (mcon.State == ConnectionState.Closed)
                {
                    mcon.Open();
                }
            }

            //-----close connection-----//
            public void closeCon()
            {
                if (mcon.State == ConnectionState.Open)
                {
                    mcon.Close();
                }
            }
        }

I really have no idea how to setup a MySQL connection properly and this was my (failed) best guess.

here is a new picture that might help http://prntscr.com/bgubj5

Nate
  • 53
  • 2
  • 8

2 Answers2

0

What exceptions does it throw? Build the connection string first. Set a breakpoint and check if it looks good. Or an even better approach will be to use the MySqlConnectionStringBuilder object

your code will look like this:

MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
conn_string.Server = serverTextBox.Text;
conn_string.UserID = userTextBox.Text;
conn_string.Password = passwordtextBox.Text;
conn_string.Database = dataBaseTextBox.Text;

using (MySqlConnection conn = new MySqlConnection(conn_string.ToString()))
using (MySqlCommand cmd = conn.CreateCommand())
{    
     //query whatever you want, be aware of SQL injection
}
vgwizardx
  • 282
  • 1
  • 3
  • 9
Alex
  • 3,689
  • 1
  • 21
  • 32
0

There should be a lot of reasons of Your problem. You posted only small piece of code, which is not enaught. If You want to connect do database consider following steps: -make proper connection string (it depends on the database) -connection string can be make from user inputs, but must be known before calling MySqlConnection

so, firstly save the user inputs to variable, make connectionstring of them, and finally pass it to MySqlConnection constructor

PS. this would help with making connectionstring: https://www.connectionstrings.com/