The purpose of this programming tutorial is to provide the flexibility in configuring the IP Address on a textbox outside the interface instead of configuring it inside the source code. This could make it much more easier when running a setup and later change the IP address from the host database workstation if the host IP change in time .
To start,
Create a database named configdb and the database table as testcon (you can change your own database name and table name if you want too).
To begin, first of all,
- Install WAMP or XAMPP Server on your computer (I used WampServer).
- Open the WAMP Server to access phpMyAdmin and create the database and the table name above.
- Name the fields or tuples in the table testcon (testconId for PK (Primary Key ) and testconnection as the first row in the table)
Configure MySQL Connection in Visual Studio 2010 (I used 2012)
- Download MySQL Connector 6.05 or 6.07 version and Install it in your computer.
- Set your connection in the Studio on the left where it says Data Source and add a New Data Source.
- Create a new project and name whatever you like. I have named it Configure_IPAdd_On_TxtBox.
- On the Solution Explorer, right-click and add a reference. Go to .NET and look for MySQL.Data DLL files and add it to the system program library.
Now, create a new project from the above step three 3, and add forms to this. So you can see from below are the two form of windows describing the configuration of any IP Addresses for MySQL Database Host depending on your database workstation IP Address.
- window GUI below show when the IP address enter is invalid
- The GUI below show when the IP address enter is valid
- After the successful connection was made, a new pop up window will show. This will indicates that your MySQL database is valid and open for any data transaction. Therefore, we are going to confirm the valid connection by testing an insert query into the MySQL Database.
- After the successful testing, below is the result shown in the database.
Below are all the codes for the above-displayed window interface.
Interface 1 (Form1)
- using System.Windows.Forms;
- using MySql.Data.MySqlClient;
-
- internal class GetConnectionString {
- public static string InTextBoxSetting {
- get;
- set;
- }
- }
-
- public string MakingConnectionString(string connection) {
- connection = "datasource='" + txtIPAddress.Text + "'; database=configdb; username=root; password=;";
- GetConnectionString.InTextBoxSetting = connection;
- MySqlConnection conn = new MySqlConnection(GetConnectionString.InTextBoxSetting);
- conn.Open();
-
- return GetConnectionString.InTextBoxSetting;
- }
-
- private void btnConnect_Click(object sender, EventArgs e) {
- try {
- if (this.txtIPAddress.Text == "127.0.0.1") {
- MakingConnectionString(GetConnectionString.InTextBoxSetting);
- MessageBox.Show("Database Host Connection succesfully ..");
- Form2 f2 = new Form2();
- f2.Show();
- } else {
- MessageBox.Show("Connection String Error");
- }
- } catch (Exception ex) {
- MessageBox.Show(ex.Message);
- }
- }
Interface 2 (Form2)
- private void btnInsert_Click(object sender, EventArgs e) {
-
- string InsertQuery = "insert into testcon(testconnection)values('" + txtData.Text + "')";
- MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(Form1.GetConnectionString.InTextBoxSetting);
- MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(InsertQuery, conn);
- conn.Open();
- MySql.Data.MySqlClient.MySqlDataReader dr = cmd.ExecuteReader();
- dr.Read();
- MessageBox.Show("Host Connected succesfully to MySql Database");
- conn.Close();
- }