当前位置:网站首页>VS2022连接sqlserver数据库教程
VS2022连接sqlserver数据库教程
2022-06-22 09:38:00 【予我心安A3】
目录
连接前准备
我们使用Visual Studio2022 连接Sqlserver数据库,我们先为我们的项目添加微软自带的连接数据库的程序集,System.Data.SqlClient。
![]()
using System.Data.SqlClient;虽然调用了,但不代表我们引用成功了,我们去检查一下Nuget程序包里,看看有没有这个程序集。

正确的安装应该如下所示 ,如果没有,这里搜出来之后选中安装即可。
打开视图下的服务器资源管理器

右键数据连接后,点击添加连接

准备要填入的信息,服务器名请打开你的SQL Server Management Studio,把服务器名称复制过来,选择相同的身份验证方式。
windows身份认证


在这一行写入连接的数据库的名字

比如我这有一个叫“考试系统数据库测试版”的数据库

填入,点击左下角的测试连接

嗨嗨嗨,接好了

可能低版本会没有这个连接测试,那么就要用代码校验了
Data Source = 服务器名;Initial Catalog = 数据库名;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
public static class data_Control
{
public static void TestConSQL()
{
//测试连接数据库
string connStr = "Data Source=LAPTOP-82MUPKTO;Initial Catalog=考试系统数据库测试版;Integrated Security=True";//自己替换一下服务器名字和数据库名字
SqlConnection conn = new SqlConnection(connStr);//这里如果报错,请退回去检查你的NUget包
//打开数据库
conn.Open();
Console.WriteLine("打开数据库连接");
//关闭数据库
conn.Close();
Console.WriteLine("关闭数据库连接");
}
}调用试一下,连接成功屏幕会输出
打开数据库连接
关闭数据库连接
data_Control.TestConSQL();sqlserver身份认证


在这一行写入连接的数据库的名字,借了上面的图,不要在意细节,假设要被连接的是这个考试系统数据库测试版


点击测试连接
嗨嗨嗨,接好了

可能低版本会没有这个连接测试,那么就要用代码校验了
Data Source = 服务器名;Initial Catalog = 数据库名;User ID = 用户名;Pwd = 密码(没有密码可以省略)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
public static class data_Control
{
public static void addtable(string tablename)
{
}
public static void TestConSQL()
{
//测试连接数据库
string connStr = "Data Source=服务器名;Initial Catalog=数据库名;User ID = 账号;Password=密码";
SqlConnection conn = new SqlConnection(connStr);//报错请检查NuGET包
//打开数据库
conn.Open();
Console.WriteLine("打开数据库连接");
//关闭数据库
conn.Close();
Console.WriteLine("关闭数据库连接");
}
}
调用试一下,连接成功屏幕会输出
打开数据库连接
关闭数据库连接

好久没更新了,期末考试终于过去了,这个夏天也要加油!
边栏推荐
- Overview of microservice architecture
- 6-7 average the surrounding elements of a two-dimensional array
- The circular queue is implemented in super detail. I understand it in a few seconds
- PAT甲级 - 1007 Maximum Subsequence Sum
- Aliyun OS上证书签发过程(certbot)
- Brush questions in C language | judge whether a certain year is only a leap year (15)
- Detr: end to end object detection with transformers (from Li Mu and Zhu)
- Redis 切片集群的数据倾斜分析
- Ctfshow web realizes killing through one question
- [ZOJ] P3228 Searching the String
猜你喜欢

Lexical Sign Sequence
![Let you get started [uni app]](/img/e7/bc33d54a345901f67afa856769045f.png)
Let you get started [uni app]

px4代码学习之基于uavcan协议的时间同步消息中添加自定义字段

Summary and Prospect of AI security technology | community essay solicitation

一个老开源人的自述-如何干好开源这件事

Value (address) transmission, see the name clearly, don't fall into the ditch

Double machine hot standby of firewall on ENSP

APM 飞行模式切换--源码详解

Basic knowledge of AI Internet of things | community essay solicitation

Byte/byte? Don't get dizzy!
随机推荐
PAT甲级 - 1015 Reversible Primes (进制转换&素数判断)
The time difference between IIS7 log and system time is 8 hours. Use logparser to solve the problem
SQL编程task02作业-基础查询与排序
使用ELK保存Syslog、Netflow日志和审计网络接口流量
File expert ---multer
Summary and Prospect of AI security technology | community essay solicitation
6-32 construction of linked list by header insertion method
Lock reentrantlock
呼叫中心专业术语
Centos7安装PostgreSQL12
Stream stream creation_ Operation_ Collection_ case
Brush questions in C language | judge whether a certain year is only a leap year (12)
Unicode characters / static non static access
char[],char *,string之间转换
Shengdun technology joined dragon lizard community to build a new open source ecosystem
双指针仪表盘读数(二) ——表盘位置识别
拉开安全距离:国际空间站采取了主动避让太空垃圾碎片的措施
微服务架构概述
谁说postgresql 没有靠谱的高可用(2)
Network security Kali penetration learning how to conduct Nessus vulnerability detection