首先在官网下载,mysql-connect-net,用于使用mysql的驱动程序,我在下载mysql-connect-net.msi. installer后,执行安装程序的时候一直无法安装成功,最简单的方法是直接下载.zip文件后解压,无须安装。

官网地址:http://dev.mysql.com/downloads/file/?id=463757

解压文件后,

出现了好几个文件夹,其中有v4和v4.5两个文件夹,对应vs的不同版本

VS2010使用V4.0下的dll文件
VS2012/2013/2015使用v4.5下的dll文件

其中有一个帮助手册十分有用:

Documentation文件夹下的ConnectorNET.chm中包含了连接mysql数据库的API。

MySqlConnection类用来连接数据库

Constructor:

构造函数

  MySqlConnection(String)
Initializes a new instance of theMySqlConnection class when given a string containing the
connection string. 

methods:

打开数据库

  Open
Opens a database connection with the property settings specified by the ConnectionString.

(Overrides DbConnection.Open().)

关闭数据库

  Close
Closes the connection to the database. This is the preferred method of closing any open connection.

(Overrides DbConnection.Close().)

结果在编译运行的时候,出现警告

请将项目文件中的“AutoGenerateBindingRedirects”属性设置为 true

这个可以找到vs该工程的文件夹下的csproj文件中增加<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

警告即可消除。

也可以参考该网址http://www.cnblogs.com/zoro-zero/p/5867320.html解决问题

constructor:

  MySqlCommand(String,
MySqlConnection)
Initializes a new instance of theMySqlCommand class with the text of the query and aMySqlConnection
  MySqlCommand(String,
MySqlConnection, MySqlTransaction)
Initializes a new instance of theMySqlCommand class with the text of the query, aMySqlConnection,
and theMySqlTransaction.  

methods:

执行sql语句

  ExecuteNonQuery
Executes a SQL statement against the connection and returns the number of rows affected.

(Overrides DbCommand.ExecuteNonQuery().)

property:

设置或返回sql语句

CommandText
Gets or sets the SQL statement to execute at the data source.

(Overrides DbCommand.CommandText.)

通过改变CommandText内容,使MysqlCommand类能够执行多条sql语句。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //////////////////////////////////////
  7. using MySql.Data;
  8. using MySql.Data.MySqlClient;
  9. //建议使用mysqlClient模式,如果连接的数据库是mysql的话
  10. //在C#中,如果想连接数据库的话,需要使用Connection连接对象。同样,不同的连接模式下,所使用的连接对象也不同
  11. //还有三种连接方式
  12. //(1)System.Data.SqlClient模式,使用sqlServer数据库比较好
  13. //(2) System.Data.OleDb模式
  14. //(3) System.Data.Odbc模式
  15. //<1>如果使用MsqlClient模式的话,其基本连接字符串和连接对象如下:
  16. //连接字符串:string connectString = "server=localhost;User Id=root;password=;Database=testDB";
  17. //属性server是指数据库所在的机器(服务器)的IP地址,如果使用当前机器(本地机器)的话,也就是使用自己电脑上的数据库的话,可以使用"localhost"或者"127.0.0.1",如果使用其它机器上的数据库的话,使用那台机器的IP地址。
  18. //database指的数据库的名字。
  19. //Id代表连接数据库的用户名
  20. //password代表连接数据库的密码,如果密码为空的话不需要填写,这样写"password="即可。
  21. namespace CsharpMysql
  22. {
  23. class Program
  24. {
  25. static void Main(string[] args)
  26. {
  27. string constructorString = "server=localhost;User Id=root;password=;Database=test";
  28. MySqlConnection myConnnect = new MySqlConnection(constructorString);
  29. myConnnect.Open();
  30. MySqlCommand myCmd = new MySqlCommand("insert into user(name,year) values('jjj',22)", myConnnect);
  31. Console.WriteLine(myCmd.CommandText);
  32. if (myCmd.ExecuteNonQuery() > 0)
  33. {
  34. Console.WriteLine("数据插入成功!");
  35. }
  36. myCmd.CommandText = "insert into user(name,year) values('jjj4',22)";
  37. Console.WriteLine(myCmd.CommandText);
  38. if (myCmd.ExecuteNonQuery() > 0)
  39. {
  40. Console.WriteLine("数据插入成功!");
  41. }
  42. myCmd.CommandText = "delete from user";
  43. Console.WriteLine(myCmd.CommandText);
  44. if (myCmd.ExecuteNonQuery() > 0)
  45. {
  46. Console.WriteLine("user表类型数据全部删除成功!");
  47. }
  48. myCmd.Dispose();
  49. myConnnect.Close();
  50. }
  51. }
  52. }<pre name="code" class="csharp">

参考:

http://blog.csdn.net/cfl20121314/article/details/27106819
http://blog.csdn.net/zhanghaoliangdehao/article/details/7372550

http://blog.csdn.net/apache6/article/details/2778878

.net连接mysql的更多相关文章

  1. nodejs进阶(6)—连接MySQL数据库

    1. 建库连库 连接MySQL数据库需要安装支持 npm install mysql 我们需要提前安装按mysql sever端 建一个数据库mydb1 mysql> CREATE DATABA ...

  2. 【初学python】使用python连接mysql数据查询结果并显示

    因为测试工作经常需要与后台数据库进行数据比较和统计,所以采用python编写连接数据库脚本方便测试,提高工作效率,脚本如下(python连接mysql需要引入第三方库MySQLdb,百度下载安装) # ...

  3. Node.js Express连接mysql完整的登陆注册系统(windows)

    windows学习环境: node 版本: v0.10.35 express版本:4.10.0 mysql版本:5.6.21-log 第一部分:安装node .Express(win8系统 需要&qu ...

  4. PDO连接mysql数据库

    1.PDO简介 PDO(PHP Data Object) 是PHP 5 中加入的东西,是PHP 5新加入的一个重大功能,因为在PHP 5以前的php4/php3都是一堆的数据库扩展来跟各个数据库的连接 ...

  5. 使用ABP EntityFramework连接MySQL数据库

    ASP.NET Boilerplate(简称ABP)是在.Net平台下一个很流行的DDD框架,该框架已经为我们提供了大量的函数,非常方便与搭建企业应用. 关于这个框架的介绍我就不多说,有兴趣的可以参见 ...

  6. jmeter之连接mysql和SQL Server配置

    下载jdbc驱动 在使用jmeter做性能或自动化测试的时候,往往需要直接对数据库施加压力,或者某些参数只能从数据库获取,这时候就必须使用jmeter连接数据库. 1.下载对应的驱动包 mysql驱动 ...

  7. Windows操作系统下远程连接MySQL数据库

    用Eclipse做一个后台项目,但是数据库不想放在本地电脑,于是买了一个腾讯云服务器(学生有优惠,挺便宜的),装上MySQL数据库,但是测试连接的时候,发现总是连接不是上,但是本地数据库可以连接,于是 ...

  8. Hibernate连接mysql数据库的配置

    <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...

  9. C# 3种方法连接MySql

    转   http://wenku.baidu.com/view/d0cf34708e9951e79b8927c7.html C# 连接MYSQL数据库的方法及示例 连接MYSQL数据库的方法及示例 方 ...

  10. cmd连接mysql的方法详解(转载)

    连接:mysql -h主机地址 -u用户名 -p用户密码 (注:u与root可以不用加空格,其它也一样)断开:exit (回车) 创建授权:grant select on 数据库.* to 用户名@登 ...

随机推荐

  1. python从入门到精通之30天快速学python视频教程

    点击了解更多Python课程>>> python从入门到精通之30天快速学python视频教程 课程目录: python入门教程-1-Python编程语言历史及特性.mkv pyth ...

  2. Centos 6版本Device eth0 does not seem to be present,delaying initialization.故障处理

    1.1  故障现象 2019年06月14日晚上,公司项目组说有台业务服务器连接不上,比较着急,我通过vpn拨入的方式远程登录到管理控制台查看发现网卡没有获取到IP地址,我尝试重启来重新启动,重启的时候 ...

  3. Django基于类的增删改查,简单逻辑都不用写

    Django是Python中一个非常牛逼的web框架,他帮我们做了很多事,里边也提前封装了很多牛逼的功能,用起来简直不要太爽,在写网站的过程中,增删改查这几个基本的功能我们是经常会用到,Django把 ...

  4. 在Ubuntu下配置jdk+maven

    1.在官网上下载对应Linux版本的jdk 2.在终端输入命令将下载好的jdk解压并且转移到指定的路径如:首先执行 tar -zxvf jdk-8u181-linux-x64.tar.gz    然后 ...

  5. configParser模块详谈

    前言 使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser configPars ...

  6. windows下,cmd 运行 python 脚本,选中文字就停止运行了【已解决】

    参考资料: https://jingyan.baidu.com/article/ce09321bb95dda2bff858f26.html 问题原因: cmd 里面,快速编辑模式会暂停程序 解决步骤: ...

  7. Falsk

    flask: 1.配置文件的几种方式: 1.app.config['DEBUG'] =True 2.app.config.from_pyfile("setting.py") 3.a ...

  8. 使用 Dom4j 将 XML 转换为 MAP

    本文为转载:http://blog.sina.com.cn/s/blog_6145ed810100z164.html  原文地址. 自己仅作备忘录方便查找留了一份. 这是解析Xml 的辅助类 pack ...

  9. SPFA - Luogu 3385 【模板】负环

    [模板]负环 描述 找负环 输入 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M,表示图有N个顶点,M条边 接下来M行,每行三个整数a b w,表示a->b有一条权值为w ...

  10. 【Trapping Rain Water】cpp

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...