当前位置:网站首页>C#使用MySql进行操作
C#使用MySql进行操作
2022-06-25 22:44:00 【应用猎人】
1.安装Mysql(网上教程有)
2.创建项目webapi
3.引用dll一般在(安装MySQL就有的没有参考:ASP.NET 连接MySQL数据库 详细步骤 - wusir - 博客园):C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.23\Assemblies\v4.5.2\MySql.Data.dll
4.代码:(连接-》查询-》格式化获取MySql的数据)
public static void GetMySqlData()
{
MySqlConnection conn = null;//创建MySqlConnection类
conn = new MySqlConnection("server=localhost;user id=root;password=123456;database=magessystemdb; pooling=true;");//创建连接字符串,提供连接数据库的必要信息
conn.Open();//连接数据库
if (conn.State.ToString() == "Open")//检测是否连接成功
{
Console.WriteLine("连接MySQL数据库成功");
//MySqlCommand cmd = new MySqlCommand("insert into use values(0,'hubuyu')", conn);//创建MySqlCommand类,并提供SQL语句,以及要操作的 MySqlConnection对象
//int n = cmd.ExecuteNonQuery();//执行SQL语句,对连接的数据库进行操作,ExecuteNonQuery()方法不能用于查询语句
// Console.WriteLine("影响行数:" + n);//查看返回值,为影响到的行数
MySqlCommand cmd = new MySqlCommand("select*from `use`", conn);
int n = cmd.ExecuteNonQuery();
Console.WriteLine("选择一种查询方式:1为使用MySqlDataReader类进行查询,2为使用ADO.NET(MySqlDataAdapter,DataSet,DataTable类)进行查询:");
int s = 2;
if (s == 1)
{
MySqlDataReader dr;//如果想要使用查询语句,则应先创建MySqlDataReader类,此为ExecuteReader()方法的返回类型
dr = cmd.ExecuteReader();//使用ExecuteReader()方法执行查询语句
while (dr.Read())//使用Read()方法查询返回的MySqlDataReader对象中的数据,返回值为true或false
{
Console.WriteLine(dr["Id"] + " " + dr["Name"]);//输出查询到的数据,dr["字段名"],这里的字段名对应数据库查询的表中的字段名
}
dr.Close();//关键的一步,不要忘记关闭MySqlDataReader对象!!!!!
}
else if (s == 2)
{
MySqlDataAdapter da = new MySqlDataAdapter(cmd);//第二种查询方式,使用MySqlDataAdapter对象
DataSet ds = new DataSet();//创建 DataSet对象
DataTable dt = new DataTable();//创建 DataTable对象
da.Fill(ds, "use");//调用Fill方法执行SQL语句,并将查询存入DataSet对象中
dt = ds.Tables["use"];//对数据库检索后,被取回的数据就存放在DataTable对象中
for (int i = 0; i < dt.Rows.Count; i++)//读取数据
{
var Id = dt.Rows[i]["Id"].ToString();
var Name = dt.Rows[i]["Name"].ToString();
Console.WriteLine(dt.Rows[i]["Id"].ToString() + " " + dt.Rows[i]["Name"].ToString());
}
}
}
else
{
Console.WriteLine("连接失败");
}
conn.Close();//关键的一步,不要忘记关闭连接!!!!!
}
5.后期可以结合SQL语句动态生成和和动态映射成实体类
6.这样就能操作MySql数据库了
7.遇到的坑:MySql.Data的版本要和对应的.net Framework对应:Mysql 8.0.23对应.net 4.7.2
8.MySql工具:https://download.csdn.net/download/weixin_42401291/15384716
9:vs代码:MySqlManageSystem.zip-MySQL文档类资源-CSDN下载
---------------------------20220506-------------------------------
重新认识
EF Core 的数据库管理
1.通过指令生成数据库或者更新创建,删除数据的表
EF Core 工具参考(包管理器控制台)- EF Core | Microsoft Docs
边栏推荐
- Chapter V exercises (124, 678, 15, 19, 22) [microcomputer principles] [exercises]
- 213. house raiding II
- Phoenix index
- Function and principle of SPI solder paste inspection machine
- Permission design = function permission + Data permission
- 1-9Vmware中网络配置
- 11.1.2 overview of Flink_ Wordcount case
- SMT Mounter workflow
- DPVS fullnat mode management
- SQL to retain the maximum value sorted by a field
猜你喜欢
"Method not allowed", 405 problem analysis and solution
Permission design = function permission + Data permission
学习识别对话式问答中的后续问题
ciscn_2019_en_2
Final review [machine learning]
mtb13_ Perform extract_ blend_ Super{candidate (primaryalternate) \u unique (nullable filtering \foreign\index\granulati
Mining pit record of modified field information in Dameng database
Chapter VI exercises (678) [microcomputer principles] [exercises]
Display unassigned virtual address after easyconnect connection
Typescript for Web Learning
随机推荐
Regular expression introduction and some syntax
Flink报错:Error: A JNI error has occurred, please check your installation and try again
Compile the telegraph desktop side (tdesktop) using vs2022
Qt优秀开源项目之九:qTox
CaMKIIa和GCaMP6f是一樣的嘛?
Idea kotlin version upgrade
1-9Vmware中网络配置
What do SMT operators do? operating duty?
Drag the mouse to rotate the display around an object
Learn to identify follow-up questions in dialogue Q & A
ASP.NET cache缓存的用法
jarvisoj_level2_x64
. user. PHP website installation problems caused by INI files
react + router 框架下的路由跳转后缓存页面存初始参数
Display unassigned virtual address after easyconnect connection
1-10vmware builds customized network architecture
渲云携手英特尔,共创云渲染“芯”时代
Ad20 (Altium designer) PCB highlight network
Post ordered clue binary tree
Solution to component stele in SMT chip processing