C#连接mariadb(MYSQL分支)代码示例分享

时间:2024-01-14 01:09:33 

首先配置好你的MariaDb,创建test数据库,在test里创建MyTable表,脚本如下(通过HeidiSQL导出的脚本):


-- --------------------------------------------------------
-- 主机:                           172.16.40.153
-- 服务器版本:                        5.5.5-10.0.4-MariaDB-1~wheezy-log - mariadb.org binary distribution
-- 服务器操作系统:                      debian-linux-gnu
-- HeidiSQL 版本:                  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' */;

-- 导出 test 的数据库结构
DROP DATABASE IF EXISTS `test`;
CREATE DATABASE IF NOT EXISTS `test` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `test`;

 
-- 导出  表 test.MyTable 结构
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;

-- 正在导出表  test.MyTable 的数据:~0 rows (大约)
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 */;

C#代码如下:包含了简单的新增删除查询,工具是vs2012版本,一定要下载mysql-connector-net,我选择的是最新的版本

别忘了添加引用


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");

        }

标签:mariadb,MYSQL分支
0
投稿

猜你喜欢

  • oracle 日期函数集合(集中版本)第1/2页

    2009-06-19 17:23:00
  • 对pandas中iloc,loc取数据差别及按条件取值的方法详解

    2021-06-15 01:58:05
  • Mysql存储过程学习笔记--建立简单的存储过程

    2024-01-23 14:41:21
  • 如何使用图片精灵优化你的网站

    2009-07-21 11:35:00
  • asp学习入门基本语法知识

    2007-11-07 14:02:00
  • thinkphp控制器调度使用示例

    2023-11-16 12:34:15
  • ionic在开发ios系统微信时键盘挡住输入框的解决方法(键盘弹出问题)

    2024-05-02 16:18:12
  • C#实现按数据库邮件列表发送邮件的方法

    2024-01-24 02:37:32
  • Python实现二分查找算法实例

    2022-04-07 08:44:37
  • 详解Python OpenCV图像分割算法的实现

    2022-11-28 13:15:44
  • FrontPage XP设计教程5——表单的设计

    2008-10-11 12:35:00
  • python面向对象实现名片管理系统文件版

    2022-11-25 16:27:45
  • 如何将ChatGPT整合到Word中

    2023-12-20 03:13:54
  • python列表的常用操作方法小结

    2023-06-21 16:14:34
  • go语言中切片的长度和容量的区别

    2024-04-27 15:25:13
  • sqlserver 日期比较、日期查询常用语句:月的第一天,季度的第一天等

    2010-08-01 18:58:00
  • js/jq仿window文件夹移动/剪切/复制等操作代码

    2024-04-16 09:50:01
  • 解决echarts中饼图标签重叠的问题

    2021-10-22 03:33:33
  • 对Python强大的可变参数传递机制详解

    2021-06-23 02:54:09
  • Java中Pattern用法实例(正则表达式)

    2023-05-19 15:02:57
  • asp之家 网络编程 m.aspxhome.com