手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

C#基于MySQL驱动(VS2012)操作MariaDB

[日期:2014-11-06] 来源:Linux社区  作者:xxdddail [字体:大 中 小]

MariaDB最近越来越热,于是也下了个来捣腾捣腾。我下的是mariadb-10.0.4-win32.msi,但是安装完后,想用C#进行连接时,发现没有可以用的驱动。网上找了一番后,说是MaraiDB兼容MySQL,所以可以用MySQL的驱动来连接。不过,有文提到MariaDB5.5相当于MySQL的5.5,而MariaDB10.0.4相当于MySQL5.6,而MySQL现在已经是6.7了,所以下载MySQL驱动的时候要特别注意,还有要注意的是mariadb是32还是64位的,搞错了,连接会出问题。

在下MySQL驱动的时候本来想在官网下的,但是要帐号,所以就另找了个镜像点下。这是镜像点http://mirrors.sohu.com/MySQL/Connector-Net/,我下的是MySQL-connector-net-5.2.7。

下面是具体实现的代码

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

namespace MariaDBTest
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        MySqlConnection connection_;
        private void buttonOpenConnect_Click(object sender, EventArgs e)
        {
            string connectionStr = "server=localhost;user id=root;password=abc;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 no = DateTime.Now.Second;
            int sum = DateTime.Now.Millisecond;
            string sql = string.Format("INSERT INTO MyTable (`NO`,`Sum`) VALUES({0},{1});", no, sum);
            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 `NO`={0}", no);
            MySqlCommand command = new MySqlCommand(sql, connection_);
            int affectLines = command.ExecuteNonQuery();

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

        }

   
    }
}

这里有几点需要注意的:

1、用MySql操作的语言和操作MSSQL的语法没有太大的差异,但是SQL的语句有差异。在MSSQL中单引号用的是键盘上回车键左边单引呈键,而MariaDB中的单引号用的是键盘上数字键那一行最左边的键(数字1键的左边)。最开始被这个折腾惨了,后面经过比对后才发现的。

2、代码中所用的数据库test和MyTable需要自己建立。建立的方法可以通过HeidiSQL来操作。

3、要记得引用MySql.data.dll.

4、如果发现有MariaDB的语法不太理解,可以打开HeidiSQL,然后进行相应的操作,这时在下方会显示相应的代码,如我们插入了一些数据,下面会显示插入的SQL语句,然后把这个语句来到C#中用就OK了。

C#基于MySQL驱动(VS2012)操作MariaDB

在 CentOS/RHEL/Scientific Linux 6 下安装 LAMP (Apache with MariaDB and PHP) http://www.linuxidc.com/Linux/2013-07/86755.htm

MariaDB Proxy读写分离的实现 http://www.linuxidc.com/Linux/2014-05/101306.htm

Linux下编译安装配置MariaDB数据库的方法 http://www.linuxidc.com/Linux/2014-11/109049.htm

CentOS系统使用yum安装MariaDB数据库 http://www.linuxidc.com/Linux/2014-11/109048.htm

安装MariaDB与MySQL并存 http://www.linuxidc.com/Linux/2014-11/109047.htm

MariaDB与MySQL并存时的分别登陆方法 http://www.linuxidc.com/Linux/2014-11/109050.htm

MariaDB 的详细介绍:请点这里
MariaDB 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-11/109051.htm

linux
相关资讯       MariaDB 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款