这是关于MySQL数据库的C#教程,包含了对MySQL数据库基本操作;

数据库访问组件MySql Connect/NET

MySql Connect/NET是MySQL官方提供给C#的接口,封装的非常好,操作MySQL就如同操作自家的SQLServer;

MySql Connect/NET

开始程序之旅

数据库模型层UserEntity.cs

using System;
using System.Collections.Generic;
using System.Text; namespace AccessOfMysql.Entity
{
public class MysqlConfig
{
//主机
private string server;
public string Server
{
get
{
return server;
}
set
{
server = value;
}
} //数据库
private string database;
public string Database
{
get
{
return database;
}
set
{
database = value;
}
} //用户id
private string userid;
public string Userid
{
get
{
return userid;
}
set
{
userid = value;
}
} //密码
private string password;
public string Password
{
get
{
return password;
}
set
{
password = value;
}
}
}
public class UserEntity
{
/// <summary>
/// 用户id
/// </summary>
private uint id;
public uint Id
{
get
{
return id;
}
set
{
id = value;
}
}
/// <summary>
/// 用户姓名
/// </summary>
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
/// <summary>
/// 用户住址
/// </summary>
private string address;
public string Address
{
get
{
return address;
}
set
{
address = value;
}
}
}
}

数据库访问层UserAccess.cs

using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using AccessOfMysql.Entity; namespace AccessOfMysql
{
class UserAccess
{
private string connectString; public UserAccess(string connectString)
{
this.connectString = connectString;
} public List<UserEntity> GetUserListFromDb()
{
string query = @"SELECT *
FROM t_user"; List<UserEntity> userEntityList = new List<UserEntity>(); using (MySqlConnection connection = new MySqlConnection(this.connectString))
{
//打开数据库连接
connection.Open(); //创建SqlCommand对象
MySqlCommand command = new MySqlCommand(query, connection); //执行SQL,返回查询结果
using (MySqlDataReader dataReader = command.ExecuteReader())
{
while (dataReader.Read())
{
UserEntity userEntity = new UserEntity();
userEntity.Id = dataReader.GetUInt32(0);
userEntity.Name = dataReader.GetString(1);
userEntity.Address = dataReader.GetString(2);
userEntityList.Add(userEntity);
}
}
}
return userEntityList;
} public void InserUserToDb(UserEntity user)
{
string query = @"INSERT INTO t_user (name, address)
VALUES (@name, @address)"; using (MySqlConnection connection = new MySqlConnection(this.connectString))
{
//打开数据库
connection.Open(); //创建SqlCommand
MySqlCommand command = new MySqlCommand(query, connection); command.Parameters.AddWithValue("@name", user.Name);
command.Parameters.AddWithValue("@address", user.Address); //执行SQL语句,不查询
command.ExecuteNonQuery();
}
} public int GetUserCountFromDb()
{
string query = @"SELECT COUNT(*)
FROM t_user";
using (MySqlConnection connection = new MySqlConnection(connectString))
{
//打开数据库
connection.Open(); MySqlCommand command = new MySqlCommand(query, connection);
//执行SQL,返回条目数
int count = int.Parse(command.ExecuteScalar().ToString()); return count;
}
}
}
}

主程序Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using AccessOfMysql.Entity; namespace AccessOfMysql
{
class Program
{
static void Main(string[] args)
{ //数据库连接字符串(charset=utf8:解决插入汉字乱码问题)
string connectString = "server=127.0.0.1;database=cjtdb;uid=root;pwd=root;charset=utf8";
UserAccess userAccess = new UserAccess(connectString);
try
{
//取得用户信息一览
List<UserEntity> userEntityList = userAccess.GetUserListFromDb();
foreach(UserEntity userEntity in userEntityList)
{
Console.WriteLine("id={0}, name={1}, address={2}",
userEntity.Id, userEntity.Name, userEntity.Address);
} //插入用户信息
UserEntity user = new UserEntity();
user.Name = "李小龙";
user.Address = "上海";
userAccess.InserUserToDb(user); //统计用户信息总条数
int count = userAccess.GetUserCountFromDb();
}
catch (MySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}

MySQL C#教程的更多相关文章

  1. 数据库 之MySQL 简单教程

      So Easy系列之MySQL数据库教程 1.   数据库概述 1.1.  数据库概述 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和 ...

  2. 21分钟 MySQL 入门教程(转载!!!)

    21分钟 MySQL 入门教程 目录 一.MySQL的相关概念介绍 二.Windows下MySQL的配置 配置步骤 MySQL服务的启动.停止与卸载 三.MySQL脚本的基本组成 四.MySQL中的数 ...

  3. MySQL Python教程(1)

    首先对于数据库的基本操作要有一定基础,其次了解Python的基础语法. 建议先阅读MysqL中文教程http://doc.mysql.cn/mysql5/refman-5.1-zh.html-chap ...

  4. 迅美VPS安装和配置MySQL数据库教程

    MySQL相关教程与知识:    迅美VPS安装和配置MySQL数据库教程    navicat8管理MySQL教程-创建数据库和导入数据    navicat8管理MySQL教程-管理建立用户和分配 ...

  5. 原创教程:SpagoBI4.2汉化及配置Mysql数据库教程

    SpagoBI4.2汉化及配置Mysql数据库教程 商务智能套件SpagoBI提供一个基于J2EE的框架用于管理BI对象如报表.OLAP分析.仪表盘.记分卡以及数据挖掘模型等的开源BI产品.它提供的B ...

  6. 屌炸天实战 MySQL 系列教程(二) 史上最屌、你不知道的数据库操作

    此篇写MySQL中最基础,也是最重要的操作! 第一篇:屌炸天实战 MySQL 系列教程(一) 生产标准线上环境安装配置案例及棘手问题解决 第二篇:屌炸天实战 MySQL 系列教程(二) 史上最屌.你不 ...

  7. CentOS下Mysql安装教程

    CentOS下Mysql安装教程 本人学习Linux时使用的是CentOs5.5版本,在该环境中,Mysql的安装方法有很多种,下面我只讲我这次成功了的方法,作为一个记录,供大家参考,同时给自己做一个 ...

  8. Windows之MySQL安装教程

    MySQL安装说明 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,My ...

  9. linux下彻底卸载mysql 图解教程

    linux下彻底卸载mysql 图解教程 1.查找以前是否装有mysql 命令:rpm -qa|grep -i mysql可以看到如下图的所示: 说明之前安装了:MySQL-client-5.5.25 ...

  10. Windows下安装MySQL详细教程

    Windows下安装MySQL详细教程 1.安装包下载  2.安装教程 (1)配置环境变量 (2)生成data文件 (3)安装MySQL (4)启动服务 (5)登录MySQL (6)查询用户密码 (7 ...

随机推荐

  1. 一个好的函数(gcd)求最小公约数

    这个函数是我无意中看到的很不错,很给力,我喜欢 是用于求最小公约数的 简单的描述就是,记gcd(a,b)表示非负整数a,b的最大公因数,那么:gcd(a,b)=gcd(b,a%b)或者gcd(a,0) ...

  2. MySQL ibdata多路径扩容

    vi /etc/my.cnf innodb_data_home_dir = innodb_data_file_path= /data/mysql/ibdata1:10M:autoextend(为目前i ...

  3. PHP中的date函数中时区问题

    从php5.1.0开始,php.ini里加入了date.timezone这个选项,默认情况下是关闭的,也就是显示的时间(无论用什么php命令)都是格林威治标准时间,所以才会有这个情况发生 解决方法如下 ...

  4. MyBatis 一对多,多对一关联查询的时候Mapper的顺序

    要先写association,然后写collection:这是由DTD决定的: <resultMap ...> <association ...> </associati ...

  5. 从[NOI2008志愿者招募]浅谈线性规划在网络流构图上的巧用

    首先来看一下题..http://www.lydsy.com/JudgeOnline/problem.php?id=1061 1061: [Noi2008]志愿者招募 Description 申奥成功后 ...

  6. HDU1166(分块)

    敌兵布阵 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  7. KB奇遇记(7):不靠谱的项目实施计划

    在ERP项目启动前期,项目组两方项目经理和我等几个人单独跟总裁开会,讨论了初步的ERP实施计划,本来第一期上线只是考虑上其中一家工厂而已,结果临时加入了深加工的工厂.本来项目组预定计划是2017年1月 ...

  8. Android客户端连接服务器端,向服务器端发送请求HttpURLConnection

    在Java中想后台服务器发送请求一般都直接使用了Java的网络编程,或者使用HttpClient向后台服务器端发送HTTP请求.虽然在安卓中,所有Java的API都可以使用,而却使用其并不会出现什么问 ...

  9. 使用PMD进行代码审查

    很久没写博客了,自从上次写的设计模式的博客被不知名的鹳狸猿下架了一次之后兴趣大减,那时候就没什么兴致写博客了,但是这段时间还没有停下来,最近也在研究一些其他的东西,目前有点想做点东西的打算,但好像也没 ...

  10. block和delegate的区别

    代理  可读性高  大部分可以属性 block   写的代码少 一般作为参数通知   占用资源 无论是block还是delegate模式本质上都是回调,使用block,其优点是回调的block代码块直 ...