using System;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
using System.Linq;

namespace ConsoleApp3
{
    class Program
    {
        private static Random random = new Random();

        public static string randomString(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            return new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray());
        }

        static void Main(string[] args)
        {
            string connStr = "server=127.0.0.1; user=ycz; database=honbro; password=123456; port=3306";
            MySqlConnection connection = new MySqlConnection(connStr);
            connection.Open();
            Console.WriteLine("ok");

            MySqlCommand command;
            string sql = string.Empty;

            // insert into persons(LastName, FirstName, Address, City) values("", "", "", "")
            string lastName = string.Empty;
            string firstName = string.Empty;
            string address = string.Empty;
            string city = string.Empty;

            for (int  i = 0; i < 10000000; i++)
            {
                lastName = randomString(50);
                firstName = randomString(50);
                address = randomString(50);
                city = randomString(50);

                sql = String.Format("insert into persons(LastName, FIrstName, Address, City) values('{0}', '{1}', '{2}', '{3}')", lastName, firstName, address, city);
                command = new MySqlCommand(sql, connection);
                command.ExecuteNonQuery();
                Console.WriteLine(i);
            }

            // select count(*) from persons
            sql = "select count(*) from persons";
            command = new MySqlCommand(sql, connection);
            object result = command.ExecuteScalar();
            if (result != null)
            {
                int n = Convert.ToInt32(result);
                Console.WriteLine("total column count: {0}", n);
            }

            // select * from persons where P_id = 50000
            sql = "select * from persons where P_id = 50000";
            command = new MySqlCommand(sql, connection);
            MySqlDataReader reader = command.ExecuteReader();
            if (reader.Read())
            {
                Console.WriteLine(reader[0]);
                Console.WriteLine(reader[1]);
                Console.WriteLine(reader[2]);
                Console.WriteLine(reader[3]);
                Console.WriteLine(reader[4]);
            }
            reader.Close();

            connection.Close();
            Console.ReadKey();
        }
    }
}

  

参考:https://zhuanlan.zhihu.com/p/28401873

c#操作mysql的更多相关文章

  1. ASP.NET Core 1.0 使用 Dapper 操作 MySql(包含事务)

    操作 MySql 数据库使用MySql.Data程序包(MySql 开发,其他第三方可能会有些问题). project.json 代码: { "version": "1. ...

  2. Python(九) Python 操作 MySQL 之 pysql 与 SQLAchemy

    本文针对 Python 操作 MySQL 主要使用的两种方式讲解: 原生模块 pymsql ORM框架 SQLAchemy 本章内容: pymsql 执行 sql 增\删\改\查 语句 pymsql ...

  3. EF操作MySql

    EF的CodeFrist操作MySql的提前准备: 1.安装两个包:MySql.Data和MySql.Data.Entity,在VS中程序包管理器中添加2个包.(备注需要的VS2015,并且EF6支持 ...

  4. .NET Core 使用Dapper 操作MySQL

    MySQL官方驱动:http://www.cnblogs.com/linezero/p/5806814.html .NET Core 使用Dapper 操作MySQL 数据库, .NET Core 使 ...

  5. asp.net core 1.1 升级后,操作mysql出错的解决办法。

    遇到问题 core的版本从1.0升级到1.1,操作mysql数据库,查询数据时遇到MissingMethodException问题,更新.插入操作没有问题. 如果你也遇到这个问题,请参照以下步骤进行升 ...

  6. 练习:python 操作Mysql 实现登录验证 用户权限管理

    python 操作Mysql 实现登录验证 用户权限管理

  7. Python操作MySQL

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...

  8. Python中操作mysql的pymysql模块详解

    Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...

  9. java分享第十七天-03(封装操作mysql类)

     JAVA操作mysql所需jar包:mysql-connector-java.jar代码: import java.sql.*; import java.util.ArrayList; import ...

  10. LightMysql:为方便操作MySQL而封装的Python类

    原文链接:http://www.danfengcao.info/python/2015/12/26/lightweight-python-mysql-class.html mysqldb是Python ...

随机推荐

  1. idea 启动报错问题

    Artifact SpiderServer:war exploded: Error during artifact deployment. See server log for details. 1. ...

  2. Gym - 100989G (二分法)

    There are K hours left before Agent Mahone leaves Amman! Hammouri doesn't like how things are going ...

  3. Reporting Service服务SharePoint集成模式安装配置(3、4、安装sharepoint 2010必备组件及产品)

    Reporting Service服务SharePoint集成模式安装配置 第三步和第四部 第三步 安装sharepoint 2010必备组件 1.安装SharePoint2010必备组件,执行Pre ...

  4. 强大的CSS 属性选择符 配合 stylish 屏蔽新浪微博信息流广告

    新建一条微博域名下的规则: @-moz-document domain("weibo.com") { #v6_pl_rightmod_rank,#v6_pl_rightmod_ad ...

  5. ASP.NET MVC 控制器通过继承控制器来达到 过滤 并且多了一个IAuthenticationFilter

    暂时没有用到过这个IAuthenticationFilter接口,毕竟已经有三个具体实现类了,所以这个还不知道用在哪,以后看看 20190324 需要注意!!!控制器重写方法都是被protected修 ...

  6. controller 状态码

      工具使用lombok表示.如没有使用lombok 请 删除@Getter    @Setter  并给get set方法 调用方法: 类型使用 Status   --->    public ...

  7. 来到cnblong dayone

    以前懒得做笔记,csdn上有些小随笔,但是感觉csdn上的广告和积分下载越来越无法忍受了.现在转到cnblog.希望以后能够多写一些随笔吧.也算是一种坚持.听说90天可以养成一种习惯,那么就从现在开始 ...

  8. 初探UE4中的Profiling【转】

    http://blog.ch-wind.com/ue4-profiling-preview/ Profililng是成品制作过程中非常重要的一个步骤,通过Profiling才能提高运行效率使得作品达到 ...

  9. AcWing 153. 双栈排序

    https://www.acwing.com/problem/content/155/ #include <cstring> #include <iostream> #incl ...

  10. “全栈2019”Java第八十一章:外部类能否访问嵌套接口里的成员?

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...