前言
在C#项目中,我们会经常同数据库打交道,但若一切从电脑中删除,你还记得怎么建立数据库连接吗?
正文
如何建立数据库连接?
如下图所示,当将数据库文件附加到新建好的数据库中报错

第一步:打开SSMS,右击数据库,点击附加

第二步:点击添加

第三步:找到自己的数据库文件,点击确定按钮

第四步:若有多个数据库,多添加几次即可
第五步:添加完成,进入附加数据库页面,点击确定

第六步:在主页面就能正常看到就没问题

第七步:以上报错,第一个是因为路径错误,系统无法再该路径下找到数据库文件

第二是因为没有给数据库文件夹设置可访问权限
1、右击数据库文件夹,选择属性

2、属性页面选择安全 —> 点击编辑

3、在编辑页面,确保system角色的权限是完全控制

在确保路径正确,权限为“完全控制”仍无法打开,那可能是数据库文件被设置了权限,你无权访问,一般来说下图这些选项都是置灰的,不可改

如何解决重定向问题?
如图,无法与数据库建立有效链接,说明重定向未设置

在项目文件中找到重定向.exe

双击进入数据库设置工具

1、修改服务器名为本机名或“."
本机名在打开SSMS时可以复制

2、数据库名为项目对应的数据库名
比如以上项目对应的数据库名是”WSDB1“
3、数据库用户名和密码是公司定义的,我们只需在进入SSMS时选择“SQL SERVER身份验证即可输入”

4、在数据库设置工具中输入同样的用户名的密码,点击测试链接

5、一定要保存连接

6、进入重定向设置
注意:此处数据库名与数据库设置工具中的数据库名不一致
,同样的,需要修改服务器名,以及数据库用户名和密码

7、然后保存连接,这样WMS就能正常打开了

总结
感谢诸位道友阅读本文,喜欢本文的话,给博主点赞,收藏支持一下吧,道友们的关注是我创作的莫大动力!