当前位置:网站首页>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();
边栏推荐
- Explain mesh Collider in unity
- 【input 身份证号】星号 代替,input 切割成 多个 小格格(类似)
- Space shooting lesson 09: elf animation
- 【云原生】什么是 CI/CD ? | 摆平交付障碍的 CI/CD
- 什么是低代码?哪些平台适合业务人员?用来开发系统靠不靠谱?
- Space game Lesson 12: shield
- MoCo V3:视觉自监督迎来Transformer
- BUUCTF做题Upload-Labs记录pass-11~pass-20
- 向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
- Integrating database Ecology: using eventbridge to build CDC applications
猜你喜欢

Eureka相互注册,只显示对方或只在一个中显示问题

Ctfshow network lost track record (2)

It is not only convenient, safe + intelligent, but also beautiful. Fluorite releases the Big Dipper face lock dl30f and Aurora face video lock y3000fv

Interesting pictures and words

什么是 CI/CD? | 实现更快更好的软件交付

Applet container technology improves mobile R & D efficiency by 500%

EfficientFormer:轻量化ViT Backbone

Unit editor details

Job CE

Space shooting Lesson 13: explosion effect
随机推荐
速卖通测评自养号,国外环境如何搭建?需要多少成本?
SharkTeam完成Flow生态NFT市场MatrixMarket的安全审计
Observer mode, object pool
小程序容器技术,让移动研发效率提升500%
Space shooting Lesson 15: props
MobileViT:挑战MobileNet端侧霸主
58岁安徽人,干出瑞士今年最大IPO 投资界
4.1 Member的各种调用方式
C # basic 4-written examination question 1
Unit editor details
C # basic 7-iterator and coroutine
MySQL修改端口号(修改mysql的端口号会有问题吗)
Moco V1: the visual field can also be self supervised
Eureka相互注册,只显示对方或只在一个中显示问题
什么是“安全感”?沃尔沃用它自己独特的理解以及行动来告诉你
Applet container technology improves mobile R & D efficiency by 500%
PostgreSQL数据库删库前是不是需要把所有连接断开才能删除?
A 58 year old native of Anhui Province, he has become the largest IPO investor in Switzerland this year
SQL audit tool self introduction owls
Cobal Strike的学习与使用