MySQL C#教程
这是关于MySQL数据库的C#教程,包含了对MySQL数据库基本操作;
数据库访问组件MySql Connect/NET
MySql Connect/NET是MySQL官方提供给C#的接口,封装的非常好,操作MySQL就如同操作自家的SQLServer;
开始程序之旅
数据库模型层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#教程的更多相关文章
- 数据库 之MySQL 简单教程
So Easy系列之MySQL数据库教程 1. 数据库概述 1.1. 数据库概述 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和 ...
- 21分钟 MySQL 入门教程(转载!!!)
21分钟 MySQL 入门教程 目录 一.MySQL的相关概念介绍 二.Windows下MySQL的配置 配置步骤 MySQL服务的启动.停止与卸载 三.MySQL脚本的基本组成 四.MySQL中的数 ...
- MySQL Python教程(1)
首先对于数据库的基本操作要有一定基础,其次了解Python的基础语法. 建议先阅读MysqL中文教程http://doc.mysql.cn/mysql5/refman-5.1-zh.html-chap ...
- 迅美VPS安装和配置MySQL数据库教程
MySQL相关教程与知识: 迅美VPS安装和配置MySQL数据库教程 navicat8管理MySQL教程-创建数据库和导入数据 navicat8管理MySQL教程-管理建立用户和分配 ...
- 原创教程:SpagoBI4.2汉化及配置Mysql数据库教程
SpagoBI4.2汉化及配置Mysql数据库教程 商务智能套件SpagoBI提供一个基于J2EE的框架用于管理BI对象如报表.OLAP分析.仪表盘.记分卡以及数据挖掘模型等的开源BI产品.它提供的B ...
- 屌炸天实战 MySQL 系列教程(二) 史上最屌、你不知道的数据库操作
此篇写MySQL中最基础,也是最重要的操作! 第一篇:屌炸天实战 MySQL 系列教程(一) 生产标准线上环境安装配置案例及棘手问题解决 第二篇:屌炸天实战 MySQL 系列教程(二) 史上最屌.你不 ...
- CentOS下Mysql安装教程
CentOS下Mysql安装教程 本人学习Linux时使用的是CentOs5.5版本,在该环境中,Mysql的安装方法有很多种,下面我只讲我这次成功了的方法,作为一个记录,供大家参考,同时给自己做一个 ...
- Windows之MySQL安装教程
MySQL安装说明 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,My ...
- linux下彻底卸载mysql 图解教程
linux下彻底卸载mysql 图解教程 1.查找以前是否装有mysql 命令:rpm -qa|grep -i mysql可以看到如下图的所示: 说明之前安装了:MySQL-client-5.5.25 ...
- Windows下安装MySQL详细教程
Windows下安装MySQL详细教程 1.安装包下载 2.安装教程 (1)配置环境变量 (2)生成data文件 (3)安装MySQL (4)启动服务 (5)登录MySQL (6)查询用户密码 (7 ...
随机推荐
- java_web学习(8)会话与状态管
HTTP简介 WEB浏览器与WEB服务器之间的一问一答的交互过程必须遵循一定的规则,这个规则就是HTTP协议.HTTP是hypertext transfer protocol(超文本传输协 ...
- IndexAction.java (Java之负基础实战)
生成Get and Set 方法: 例如:public String view; 右击view > Source > Generate Getters and Setters...
- MalformedObjectNameException: Invalid character '' in value part of property
http://blog.csdn.net/getdate/article/details/6729706 ojdbc6.jar的问题: 最近在项目中用spring配置oracle数据库连接池, 启动的 ...
- tomcat服务器搭建之ngrok——将内网地址映射到外网
最近心血来潮,想学习微信公众号开发.但是自己又没有外网服务器,这个给我带来很大的麻烦. 刚开始申请了新浪云服务和百度云服务,将写好的代码打包丢到相应的云服务器上就行. 但问题就来了,开发中避免不了错误 ...
- 各个浏览器开启CSS Grid Layout的方式
2017年3月,Chrome.Firefox将开启默认支持. 当然对于很多人等不及浏览器默认支持,想提前体验一把,这里提供一些打开方式: 1.Chrome 在浏览器中输入:chrome://flags ...
- 使用PHP的strstr()函数来统计一段字符串中元音字母的个数(区分大小写)
<?php/**练习:统计一段字符串中所有元音字母的个数(区分大小写)*/$str='This is a test file.'; //原始字符串echo $str.'<br>'; ...
- http-server 使用介绍
做一个项目的时候需要服务环境,又不想使用apache,php,于是找到一款比较简单的易用的webserver 就是http-server 首先介绍一个怎么使用吧,http-server 是基于node ...
- display的none与block(判断登录界面的账号密码是否为空)
判断登录界面的账号密码是否为空的时候又不想用alert显示就需要用display来隐藏alert啦(在设置时切忌要将隐藏的内容写在账号和密码的div中,否则会根据屏幕的分辨率不同而有所变化,这是本人教 ...
- 5个步骤创建你的第一个RESTFul 服务
1.啥是RESTFul 服务 在我们创建简单小程序前,先来学习下RESTFul 服务.RESTFul服务就是遵循了 Representational State Transfer(可以参考http:/ ...
- 数字信号处理MATLAB简单序列
数字信号处理应用的几个基本序列: 1 单位样本序列 function mainImseq() clc clear disp('生成抽样序列'); y=imseq(,,); %调用样本函数,此时序列下标 ...