简体   繁体   English

C# Sql 连接字符串,试图从另一台计算机连接到本地数据库

[英]C# Sql connection string, trying to connect to local db from another computer

I have a local SQL Server database that I'm trying to connect to via the connection string in my application in C#.我有一个本地 SQL 服务器数据库,我试图通过 C# 中的应用程序中的连接字符串连接到该数据库。

I have tried with this connection string, I have left Password blank because I have no password given in SQL login credentials:我已经尝试使用此连接字符串,我将密码留空,因为我没有在 SQL 登录凭据中提供密码:

Data Source=myComputerIP,1433;Network Library=DBMSSOCN; Initial 
Catalog=myDataBase;User ID=myUsername;Password=;

I have enabled TCP/IP and started SQL Web Browser in SQL Server configuration manager, opened port 1433, SQL Server is configured to allow remote connections.我已启用 TCP/IP 并在 SQL 服务器配置管理器中启动 SQL Web 浏览器,打开端口 1433,SQL 服务器配置为允许远程连接。 I'm getting an error in C#我在 C# 中遇到错误

A.network related or instance-specific error occurred while establishing the connection to the server. A.建立与服务器的连接时发生网络相关或特定于实例的错误。

Am I using wrong connection string?我使用了错误的连接字符串吗?

My login to SQL Server DB:我登录到 SQL 服务器数据库:登录信息

You are using Windows Authentication so you must set also Integrated Security = true. 您正在使用Windows身份验证,因此还必须设置Integrated Security = true。 Remove the User ID and Password. 删除用户ID和密码。

With Integrated Security = true, the connection string will get the credentials of the user who runs the application 如果Integrated Security = true,则连接字符串将获取运行该应用程序的用户的凭据

from your attached PNG, it appears you're using SQLExpress. 从附加的PNG中,您似乎正在使用SQLExpress。 By Default this doesn't start on TCP port 1433. Check the ports in the Configuration Manager->Network Config->ProtocolsFor...-> right-click TCP/IP, choose properties and click IP Addresses tab 默认情况下,它不会在TCP端口1433上启动。检查Configuration Manager->网络配置-> ProtocolsFor ...->中的端口,右键单击TCP / IP,选择属性 ,然后单击IP地址选项卡。

Scroll to the bottom to set TCP Port under IpAll section to 1433, restart SQL Server, then try again (or use whatever ports appear to be defined here in your connection string) 滚动到底部,以将“ IpAll”部分下的“ TCP端口”设置为1433,重新启动SQL Server,然后重试(或使用在连接字符串中似乎在此定义的任何端口)

I'd also recommend using SQL Server users for remote clients rather than windows - this way it's nicely separated from windows and domains etc. 我还建议将SQL Server用户用于远程客户端而不是Windows-这样,它就可以与Windows和域等很好地分开。

Make sure your SQL Server is configured for mixed mode: https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/change-server-authentication-mode?view=sql-server-2017 确保将SQL Server配置为混合模式: https : //docs.microsoft.com/zh-cn/sql/database-engine/configure-windows/change-server-authentication-mode?view= sql-server- 2017

you can then create a LOGIN (for the database server) and a USER (for the specific database you want them to access) via SSMS, or you could use this script: 然后,您可以通过SSMS创建一个LOGIN(对于数据库服务器)和一个USER(对于您希望他们访问的特定数据库),或者您可以使用以下脚本:

-- Creates the login remoteuser with password 'Pa$$word11'.  
CREATE LOGIN remoteuser
    WITH PASSWORD = 'Pa$$word11';  
GO  

-- Creates a database user for the login created above.  
CREATE USER remoteuser FOR LOGIN remoteuser;  
GO 

--grant remoteuser 'datareader' rights
EXEC sp_addrolemember N'db_datareader', N'remoteuser';

--grant remoteuser 'datawriter' rights
EXEC sp_addrolemember N'db_datawriter', N'remoteuser';

You can do all the above through SSMS - add a login under the Security node, then in properties for the login set up database-level permissions too. 您可以通过SSMS完成上述所有操作-在“ 安全性”节点下添加一个登录名,然后在该登录名的属性中也设置数据库级权限。 The script is a bit more managable if you ever want to repeat the process though. 但是,如果您想重复该过程,该脚本将更具可管理性。

Connection string is then something like : 连接字符串如下所示:

Data Source=192.168.0.55\SQLExpress;Database=MyDatabase;user=remoteuser;password=Pa$$word11

I'm running a microsoft sql server in a docker container on my macbook pro from 2015, was getting this error when running my C# VS2022 API and trying to connect to the db, had to put Encrypt=True in my connection string like so: "DefaultConnection": "server=localhost;database=newcomparer;trusted_connection=false;User Id=sa;Password=reallyStrongPwd123;Persist Security Info=False;Encrypt=True"我从 2015 年开始在我的 macbook pro 上的 docker 容器中运行 Microsoft sql 服务器,在运行我的 C# VS2022 API 并尝试连接到数据库时出现此错误,必须像这样在我的连接字符串中放置Encrypt=True"DefaultConnection": "server=localhost;database=newcomparer;trusted_connection=false;User Id=sa;Password=reallyStrongPwd123;Persist Security Info=False;Encrypt=True"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM