当前位置: 移动技术网 > IT编程>开发语言>c# > C#连接mariadb(MYSQL分支)代码示例分享

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
首先配置好你的mariadb,创建test数据库,在test里创建mytable表,脚本如下(通过heidisql导出的脚本): 复制代码 代码如下:-- -------

首先配置好你的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");

        }

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网