C#: MySQL Connection & Query - DatabaseFormApp

C# MySQL Connections

  • VisualStudio makes it easy to create a webform application with buttons for DB connection & queries

Create New Project DatabaseFormApp


 

Add Reference for MySql.Data package


 


 


 

Form1.cs Form Design

Add Button

  • Name: btnConnect

  • Text: Connect!

Add Button

  • Name: btnQuery

  • Text: RunQuery

Add Label

  • Name: txtResult

  • Text: ….

Form1.cs Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace DatabaseFormApp
{
    public partial class Form1 : Form
    {
        MySqlConnection cnx = new MySqlConnection("server=192.168.0.80; port=3306; userid=outlaw1; password=outlaw1; database=outlaw; Encrypt=false;");
        MySqlCommand cmd = new MySqlCommand();
        String query = "SELECT * FROM node";
        MySqlDataReader reader;
        // for instert/delete/update ie.. non-'select' queries
        //MySqlDataReader queryResult = cmd.ExecuteQuery();
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            //String query = string.Format("SELECT * FROM node");
            try
            {
                cnx.Open();
                //MessageBox.Show("connected...");
                lblResult.Text = "Connected....";
                cnx.Close();
            }
            catch
            {
                //MessageBox.Show("NOT connected...");
                lblResult.Text = "NOT Connected....";
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                cnx.Open();
                cmd = new MySqlCommand(query, cnx);
                reader = cmd.ExecuteReader();

                StringBuilder sb = new StringBuilder();
                while (reader.Read())
                {
                    string id = (string)reader["nid"].ToString();
                    string nid = reader["nid"].ToString();
                    string type = reader["type"].ToString();
                    string uuid = reader["uuid"].ToString();
                    string result = "string = " + nid + "--" + type + "--" + uuid;
                    sb.Append(result + "\n");
                }
                lblResult.Text = sb.ToString();
                cnx.Close();
            }
            catch
            {
                lblResult.Text = "Can't Execute Query";
                //Console.WriteLine("Can't Execute Query...");
            }
        }
    }
}



 

DBForm.Designer.cs

namespace DatabaseFormApp
{
    partial class DBForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnConnect = new System.Windows.Forms.Button();
            this.lblResult = new System.Windows.Forms.Label();
            this.btnQuery = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // btnConnect
            // 
            this.btnConnect.Location = new System.Drawing.Point(12, 12);
            this.btnConnect.Name = "btnConnect";
            this.btnConnect.Size = new System.Drawing.Size(75, 23);
            this.btnConnect.TabIndex = 0;
            this.btnConnect.Text = "Connect!";
            this.btnConnect.UseVisualStyleBackColor = true;
            this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
            // 
            // lblResult
            // 
            this.lblResult.AutoSize = true;
            this.lblResult.Location = new System.Drawing.Point(93, 12);
            this.lblResult.Name = "lblResult";
            this.lblResult.Size = new System.Drawing.Size(19, 13);
            this.lblResult.TabIndex = 1;
            this.lblResult.Text = "....";
            this.lblResult.Click += new System.EventHandler(this.label1_Click);
            // 
            // btnQuery
            // 
            this.btnQuery.Location = new System.Drawing.Point(12, 42);
            this.btnQuery.Name = "btnQuery";
            this.btnQuery.Size = new System.Drawing.Size(75, 23);
            this.btnQuery.TabIndex = 2;
            this.btnQuery.Text = "RunQuery";
            this.btnQuery.UseVisualStyleBackColor = true;
            this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(483, 312);
            this.Controls.Add(this.btnQuery);
            this.Controls.Add(this.lblResult);
            this.Controls.Add(this.btnConnect);
            this.Name = "Form1";
            this.Text = "DatabaseFormApp";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button btnConnect;
        private System.Windows.Forms.Label lblResult;
        private System.Windows.Forms.Button btnQuery;
    }
}


 


 


 

Tags