当前位置:网站首页>C#语言和SQL Server数据库技术
C#语言和SQL Server数据库技术
2022-07-25 09:16:00 【yushang_zhang】
使用ADO.NET访问数据库
一、使用ADO.NET访问数据库
1.ADO.NET是 .net framework的一个重要组成部分
2.是 .net平台用于访问数据库的一种技术
3.操作数据库首先需要先连接上数据库
4.连接不同数据库要用不同的数据提供程序
5.操作SQL Server数据库使用的是SqlConnection这个类,在System.Date.SqlClient命名空间
6.连接数据库需要提供连接信息,这些连接信息编写在一个连接字符串中
a、Data Source 指定数据库所在的服务器,可以是localhost或者ip地址
b、Initial Catalog 指定要操作的数据库
c、User ID 指定用户名
d、pwd 指定密码
7.连接字符串可以通过vs来生成
8.调用连接对象的open()方法可以打开连接
9.用完记得调用close()方法关闭连接,释放资源
10.注意事项:
a、连接字符串中的各项用的是英文的分号分隔
b、服务器的IP地址、用户名和密码不要写错了
c、确保数据库服务启动了
二、使用ExecuteScalar查询数据
1.需要执行查询语句,需要将sql语句发送到数据库
2.SqlCommand对象用于将sql语句发送到数据库
3.SqlCommand对象的ExecuteScalar()方法用于查询结果中第一行第一列的值
4.注意事项:
a、操作前记得打开数据库
b、ExecuteScalar()返回的记过记得数据类型转换
三、异常处理
1.程序难免会出异常
2.出异常后,如果程序中断,这样用户体验差
3.通常可以考虑使用 try catch 进行异常捕捉
4.try包含的是可能出现异常的代码
5.如果出现异常,则进入catch()中进行处理
边栏推荐
- canvas很多圆圈组成的文本js特效
- 2022-7-14 JMeter pressure test
- 图解LeetCode——919. 完全二叉树插入器(难度:中等)
- activemq--可持久化机制之JDBC的journal
- Asp. Net core CMD common instructions
- 为什么说DAO是未来的公司形式
- Druid 查询超时配置的探究 → DataSource 和 JdbcTemplate 的 queryTimeout 到底谁生效?
- LabVIEW experiment - temperature detection system (experimental learning version)
- The development of art NFT
- centos更改mysql数据库目录
猜你喜欢
![[machine learning] Finally, the important steps of machine learning modeling have been clarified](/img/75/07767ed694502f0c910d35e1447499.jpg)
[machine learning] Finally, the important steps of machine learning modeling have been clarified

JS pop-up City filtering component matches mobile terminal

How to avoid duplicate data when the database is high and distributed

Django4.0 + web + MySQL 5.7 realize simple login operation

2022-7-14 JMeter pressure test

分享一个避免递归的部门设计方法

超赞的yolo目标检测训练所用垃圾分类数据集共享——标注好的约3000张

『每日一问』LockSupport怎么实现线程等待、唤醒

Illustration leetcode - 919. Complete binary tree inserter (difficulty: medium)

What version of Oracle10g single instance database is better to upgrade to? Ask for suggestions
随机推荐
Canvas text JS special effect composed of many circles
[BUUCTF-n1book][第二章 web进阶]SSRF Training
Redis operation uses cursor instead of keys
Troubleshooting error: NPM install emojis list failed
redis操作利用游标代替keys
Kubedm introduction
uni-app - Refused to display ‘xxx‘ in a frame because an ancestor violates the following Content Sec
Comparison between symmetric encryption and asymmetric encryption
Uniapp intercepts route jumps through addinterceptor to control whether the page needs to log in
Learn about spark project on Nebula graph
Composition of the interview must ask items
28.插槽
Silicon Valley classroom lesson 15 - Tencent cloud deployment
How does Flink SQL persist?
Asp. Net core CMD common instructions
实现简单的RESTful API服务器
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
『怎么用』观察者模式
SSM高级整合
registration status: 204