C joins mariadb of MYSQL branch code sample sharing

  • 2020-05-26 09:58:04
  • OfStack

First configure your MariaDb, create the test database, and create the MyTable table in test. The script (exported via HeidiSQL) is as follows:


-- --------------------------------------------------------
--  The host :                           172.16.40.153
--  Server version :                        5.5.5-10.0.4-MariaDB-1~wheezy-log - mariadb.org binary distribution
--  Server operating system :                      debian-linux-gnu
-- HeidiSQL  version :                  8.1.0.4545
-- --------------------------------------------------------

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;

--  export  test  Database structure 
DROP DATABASE IF EXISTS `test`;
CREATE DATABASE IF NOT EXISTS `test` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `test`;

 
--  export    table  test.MyTable  structure 
DROP TABLE IF EXISTS `MyTable`;
CREATE TABLE IF NOT EXISTS `MyTable` (
  `id` int(11) NOT NULL,
  `username` varchar(32) DEFAULT NULL,
  `password` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--  Exporting table   test.MyTable  Data: ~0 rows ( about )
DELETE FROM `MyTable`;
/*!40000 ALTER TABLE `MyTable` DISABLE KEYS */;
INSERT INTO `MyTable` (`id`, `username`, `password`) VALUES
    (1, '2013/10/13', '1f11082e-7c23-4ffd-bfd2-67b0a58d'),
    (25, '2013/10/13', 'fc52bd01-474b-4fa4-86f1-18d3a3c7'),
    (32, '2013/10/13', '1078f559-3e39-4b7d-bcab-0286c7f4'),
    (58, '2013/10/13', '95ee6ce5-fcef-4785-bd0d-3482e6de');
/*!40000 ALTER TABLE `MyTable` ENABLE KEYS */;
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

The code for C# is as follows: it contains a simple new delete query, and the tool is version vs2012. 1 must download mysql-connector-net, and I chose the latest version

Don't forget to add the reference


using MySql.Data.MySqlClient;


MySqlConnection connection_;
        private void buttonOpenConnect_Click(object sender, EventArgs e)
        {
            //string connectionStr = "server=127.0.0.1;user id=root;password=;database=test";
            string connectionStr = "server=172.16.40.153;user id=root;password=123456;database=test";
            connection_ = new MySqlConnection(connectionStr);
            connection_.Open();
            MessageBox.Show("Connect OK!");
        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            if (connection_ == null)
            {
                MessageBox.Show("Please open connect!");
                return;
            }
            string sql = "SELECT * FROM MyTable";
            MySqlDataAdapter adapter = new MySqlDataAdapter(sql, connection_);
            DataTable dataTable = new DataTable();
            adapter.Fill(dataTable);
            dataGridViewMariaDB.DataSource = dataTable;
        }

        private void buttonCloseConnect_Click(object sender, EventArgs e)
        {
            if (connection_ != null)
            {
                connection_.Close();
                MessageBox.Show("Connect Close!");
            }
        }

        private void buttonInsert_Click(object sender, EventArgs e)
        {
            if (connection_ == null)
            {
                MessageBox.Show("Please open connect!");
                return;
            }
            int id = DateTime.Now.Second;
            string username = DateTime.Now.ToShortDateString();
            string password = Guid.NewGuid().ToString();
            string sql = string.Format("INSERT INTO MyTable (`id`,`username`,`password`) VALUES({0},'{1}','{2}');", id, username, password);
            MySqlCommand command = new MySqlCommand(sql, connection_);
            int affectLines = command.ExecuteNonQuery();

            MessageBox.Show("Affect " + affectLines.ToString() + " line");

        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (connection_ == null)
            {
                MessageBox.Show("Please open connect!");
                return;
            }
            int no = Convert.ToInt32(textBoxNO.Text);
            string sql = string.Format("DELETE FROM MyTable WHERE id={0}", no);
            MySqlCommand command = new MySqlCommand(sql, connection_);
            int affectLines = command.ExecuteNonQuery();

            MessageBox.Show("Affect " + affectLines.ToString() + " line");

        }


Related articles: