VS C#SQL数据库连接字符串不适用于其他计算机 [英] VS C# SQL database connection string don`t work for other computers

查看:175
本文介绍了VS C#SQL数据库连接字符串不适用于其他计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

我创建的应用程序使用位于我的项目文件夹中的SQL Server文件数据库(.mdf)文件。当我在我的电脑,但当我尝试在其他电脑上使用时,我得到错误补丁是错误的。所以有任何原因使我的连接字符串自动生成到项目文件夹或任何其他原因。


我尝试过:



这是我的字符串:



Hi.
I create app that use SQL Server File Database (.mdf) file that is located in my project folder.App is working perfectly when i am on my PC but when i try to use at on other PC i get error that patch is wrong.So is there any why to make my Connection string auto generated to Project Folder or any other why.

What I have tried:

This is my string:

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\PC\Desktop\Projects\DApp\Database.mdf;Integrated Security=True;Connect Timeout=30");

推荐答案

您不应该硬编码数据库的路径,但将其放在用于用户数据的目录中。看一下这篇优秀文章 [ ^ ]以帮助选择一个。
You should not hard code the path to the database, but put it in a directory intended for user data. Have a look at this excellent article[^] to help choose one.


SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\PC\Desktop\Projects\DApp\Database.mdf;Integrated Security=True;Connect Timeout=30");



它不起作用,因为它指的是安装在开发计算机上的MS SQL服务器...



如果您想在LAN中共享数据库,则必须为MS SQL Server启用远程连接。有关详细信息,请参阅:配置远程访问服务器配置选项| Microsoft Docs [ ^ ]



然后你必须改变连接字符串 [ ^ ]。



想象一下,在启用远程连接之前,您是有权访问该SQL Server实例的人。换句话说,SQL服务器必须在LAN中广播它的存在;)


It won't work, because it refers to MS SQL server installed on developement computer...

If you would like to share database in LAN, you have to enable remote connections for MS SQL Server. For futher details, please see: Configure the remote access Server Configuration Option | Microsoft Docs[^]

Then you have to change connection string[^].

Imagine, you're the one who has access to that SQL Server instance till enable remote connections. In other words, SQL server have to broadcast its existence in a LAN ;)


这篇关于VS C#SQL数据库连接字符串不适用于其他计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆