.net连接mysql
首先在官网下载,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语句。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //////////////////////////////////////
- using MySql.Data;
- using MySql.Data.MySqlClient;
- //建议使用mysqlClient模式,如果连接的数据库是mysql的话
- //在C#中,如果想连接数据库的话,需要使用Connection连接对象。同样,不同的连接模式下,所使用的连接对象也不同
- //还有三种连接方式
- //(1)System.Data.SqlClient模式,使用sqlServer数据库比较好
- //(2) System.Data.OleDb模式
- //(3) System.Data.Odbc模式
- //<1>如果使用MsqlClient模式的话,其基本连接字符串和连接对象如下:
- //连接字符串:string connectString = "server=localhost;User Id=root;password=;Database=testDB";
- //属性server是指数据库所在的机器(服务器)的IP地址,如果使用当前机器(本地机器)的话,也就是使用自己电脑上的数据库的话,可以使用"localhost"或者"127.0.0.1",如果使用其它机器上的数据库的话,使用那台机器的IP地址。
- //database指的数据库的名字。
- //Id代表连接数据库的用户名
- //password代表连接数据库的密码,如果密码为空的话不需要填写,这样写"password="即可。
- namespace CsharpMysql
- {
- class Program
- {
- static void Main(string[] args)
- {
- string constructorString = "server=localhost;User Id=root;password=;Database=test";
- MySqlConnection myConnnect = new MySqlConnection(constructorString);
- myConnnect.Open();
- MySqlCommand myCmd = new MySqlCommand("insert into user(name,year) values('jjj',22)", myConnnect);
- Console.WriteLine(myCmd.CommandText);
- if (myCmd.ExecuteNonQuery() > 0)
- {
- Console.WriteLine("数据插入成功!");
- }
- myCmd.CommandText = "insert into user(name,year) values('jjj4',22)";
- Console.WriteLine(myCmd.CommandText);
- if (myCmd.ExecuteNonQuery() > 0)
- {
- Console.WriteLine("数据插入成功!");
- }
- myCmd.CommandText = "delete from user";
- Console.WriteLine(myCmd.CommandText);
- if (myCmd.ExecuteNonQuery() > 0)
- {
- Console.WriteLine("user表类型数据全部删除成功!");
- }
- myCmd.Dispose();
- myConnnect.Close();
- }
- }
- }<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的更多相关文章
- nodejs进阶(6)—连接MySQL数据库
1. 建库连库 连接MySQL数据库需要安装支持 npm install mysql 我们需要提前安装按mysql sever端 建一个数据库mydb1 mysql> CREATE DATABA ...
- 【初学python】使用python连接mysql数据查询结果并显示
因为测试工作经常需要与后台数据库进行数据比较和统计,所以采用python编写连接数据库脚本方便测试,提高工作效率,脚本如下(python连接mysql需要引入第三方库MySQLdb,百度下载安装) # ...
- Node.js Express连接mysql完整的登陆注册系统(windows)
windows学习环境: node 版本: v0.10.35 express版本:4.10.0 mysql版本:5.6.21-log 第一部分:安装node .Express(win8系统 需要&qu ...
- PDO连接mysql数据库
1.PDO简介 PDO(PHP Data Object) 是PHP 5 中加入的东西,是PHP 5新加入的一个重大功能,因为在PHP 5以前的php4/php3都是一堆的数据库扩展来跟各个数据库的连接 ...
- 使用ABP EntityFramework连接MySQL数据库
ASP.NET Boilerplate(简称ABP)是在.Net平台下一个很流行的DDD框架,该框架已经为我们提供了大量的函数,非常方便与搭建企业应用. 关于这个框架的介绍我就不多说,有兴趣的可以参见 ...
- jmeter之连接mysql和SQL Server配置
下载jdbc驱动 在使用jmeter做性能或自动化测试的时候,往往需要直接对数据库施加压力,或者某些参数只能从数据库获取,这时候就必须使用jmeter连接数据库. 1.下载对应的驱动包 mysql驱动 ...
- Windows操作系统下远程连接MySQL数据库
用Eclipse做一个后台项目,但是数据库不想放在本地电脑,于是买了一个腾讯云服务器(学生有优惠,挺便宜的),装上MySQL数据库,但是测试连接的时候,发现总是连接不是上,但是本地数据库可以连接,于是 ...
- Hibernate连接mysql数据库的配置
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...
- C# 3种方法连接MySql
转 http://wenku.baidu.com/view/d0cf34708e9951e79b8927c7.html C# 连接MYSQL数据库的方法及示例 连接MYSQL数据库的方法及示例 方 ...
- cmd连接mysql的方法详解(转载)
连接:mysql -h主机地址 -u用户名 -p用户密码 (注:u与root可以不用加空格,其它也一样)断开:exit (回车) 创建授权:grant select on 数据库.* to 用户名@登 ...
随机推荐
- videojs的使用
[官网]http://www.videojs.com/ videojs就提供了这样一套解决方案,他是一个兼容HTML5的视频播放工具,早期版本兼容所有浏览器,方法是:提供三个后缀名的视频,并在不支持h ...
- JZOJ 3470. 【NOIP2013模拟联考8】最短路(path)
470. [NOIP2013模拟联考8]最短路(path) (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Detailed ...
- python编写定时执行脚本
前几天在抓博客园文章,打算每天抓10条最新的,所以就在脚本中加了定时让它在每天凌晨四点中时执行,但是昨天发现,报错了: 显示是远程主机强制关闭了一个链接, 原因是:mysql数据库默认当连续8小时不对 ...
- 51nod 1554 KMP思维题
题目为中文,因而不再解释题意. 首先遵循如下设定可以有以下几个结论:1,首先谈论下KMP的一个特殊性质:对于某一个特立独行的字符串:例如ABCDEF,在建立有限状态自动机之后,都会有,所有元素的失配边 ...
- 笔记-python-tutorial-5.data structure
笔记-python-tutorial-5.data structure 1. data structure 1.1. list operation list.append(x) #尾部 ...
- 如何看待微软新开源的LightGBM?
GBDT虽然是个强力的模型,但却有着一个致命的缺陷,不能用类似mini batch的方式来训练,需要对数据进行无数次的遍历.如果想要速度,就需要把数据都预加载在内存中,但这样数据就会受限于内存的大小: ...
- Oracle 学习笔记(Windows 环境下安装 + PL/SQL)
Oracle 安装.PL/SQL 配置使用 前言:因更换机械硬盘为 SSD 固态硬盘装了新 Windows 7 系统,需要重新搭建开发环境,把 Oracle 安装过程和 PL/SQL 配置使用做下笔 ...
- log4j2用asyncRoot配置异步日志是如何使用disruptor
用asyncRoot配置对应的对接disruptor类是AsyncLoggerConfigDisruptor,用Log4jContextSelector启动参数配置全局异步的对应的对接disrupto ...
- linux常用命令与系统管理常用命令
linux命令:切换用户:开启ftp服务:service vsftpd start 开启ssh服务:service sshd start普通用户切换到超级用户:su rootlogout:(注销)un ...
- sqlserver释放内存
create procedure sp_clearmemasbegin dbcc freeproccache dbcc freesessioncache dbcc freesystemcache('a ...