当前位置:网站首页>C#连接MySql数据库详细步骤
C#连接MySql数据库详细步骤
2022-07-28 19:19:00 【m0_67401660】
说明:使用MySQL数据库,Navicat数据库管理工具
1、首先下载MySQL.Data.dll文件(应和对应的.netframework版本相同)
2、在引用中引入MySQL.Data.dll文件
3、在类中引入命名空间 using Mysql.Data.MySqlClient
访问并操作数据库的步骤
1、建立连接字符串
(1)private static string connString = “server=loaclhost;database=mytest;username=root;password=root;”【说明:需要将这些设置成自己电脑上的配置】
(2)通过配置文件App.config
在配置文件中添加
【注意:这里要注意各个名称以及不要有多余的空格,很可能会造成错误】
然后进行调用
private static string connString = ConfigurationMananger.ConnectionStrings[“connStrings”].ToString();
2、连接数据源
MysqlConnection conn = new MysqlConnection(connString)【connString为拼接的字符串】
3、打开连接
conn.Open()
4、对数据源执行命令
MySqlCommand command = new MySqlCommand(sql,conn)【sql为需要执行的数据库语句,conn为创建的连接】
5、从数据源中读取
(1)MySqlDataReader
MySqlDataReader reader = command.ExcuteReader();
利用reader[0]或者reader[“列名”]进行逐列读取
reader在读取完毕后要执行reader.Close()命令
(2)MySqlDataAdapter
MySqlDataAdapter长与DataSet配合使用
//使用指定的command进行初始化
MySqlDataAdapter da = new MySqlDataAdapter(command) ;
DataSet ds = new DataSet();
//在 DataSet 中添加或刷新行。
da.Fill(ds)
6、关闭数据库连接
conn.Close();
边栏推荐
- Meaning of disk status of EMC DataDomain
- Alibaba cloud MSE supports go language traffic protection
- C # basic 6-file IO and JSON
- Guo Mingxuan: meta contraction is conducive to the development of VR competitors, and apple XR headshow will change the industry rules
- Unity - Fundamentals of 3D mathematics
- Is it necessary to disconnect all connections before deleting the PostgreSQL database?
- Confusing knowledge points of software designer examination
- PostgreSQL数据库删库前是不是需要把所有连接断开才能删除?
- New development of letinar in Korea: single lens 4.55G, light efficiency up to 10%
- 小程序容器技术,让移动研发效率提升500%
猜你喜欢

《软件设计师考试》易混淆知识点

MoCo V3:视觉自监督迎来Transformer
Looking at SQL optimization from the whole process of one query

MySQL sorts out the review content -- with mind map

What functions does MySQL have? Don't look everywhere. Just look at this.
![[Zhou Zhou has a prize] cloud native programming challenge](/img/0d/e26e37cddf3cf01b5e9dcaf7211106.png)
[Zhou Zhou has a prize] cloud native programming challenge "edge container" track invites you to fight!

第六七八次作业

Unity - script lifecycle

How do we do full link grayscale on the database?

Unity foundation 2 editor expansion
随机推荐
(转)冒泡排序及优化详解
C # basic 4-written examination question 1
MySQL修改端口号(修改mysql的端口号会有问题吗)
Zcmu--5066: dark corridor
Unity3d tutorial notes - unity initial 03
Explain the imported 3D model in unity
Explain prefabrication in unity in detail
C # basic 1-events and commissions
3D laser slam: Interpretation of logo-loam paper - Introduction
Confession of a graduate student: why am I addicted to opengauss community?
The EMC vnx5200 fault light is on, but there is no hardware fault prompt
C # basic 7-iterator and coroutine
Space shooting Lesson 16: props (Part 2)
查询oracle视图创建语句及如何向视图中插入数据[通俗易懂]
上市1个月接连发生两起安全事故,理想L9还理想吗?
Explain the script data transmission and notification in unity
MobileViT:挑战MobileNet端侧霸主
Report redirect after authorized login on wechat official account_ The problem of wrong URI parameters
Unity foundation 1 - event execution sequence, custom events
How do we do full link grayscale on the database?