远程SQL Server的ConnectionString [英] ConnectionString for remote SQL Server

查看:235
本文介绍了远程SQL Server的ConnectionString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是这种情况,我想连接到位于远程服务器上的SQL2008R2,我可以远程进入服务器,并且在Windows上以管理员身份登录服务器MS(通过Windows身份验证连接到SQL)。

This is the case, I want to connect to SQL2008R2 that is located on a remote server, I can enter to the server remotely and after I login as an admin on windows I work on the SQL Server MS (The connection to SQL is through Windows Authentification).

我正在开发需要与SQL连接的WinForms应用程序,我尝试了几种connectionString都没有成功,例如:

I am working on a WinForms App that needs connection with SQL, I have tried several connectionString without success, for example:

public static string strCon = @"Server=190.xxx.xxx.xxx\ServerName,1433;Database=DataBaseName;Integrated Security=false;User ID=User;Password=Pass";

public static string strCon = @"Data Source=190.xxx.xxx.xxx\ServerName,1433;Network Library=DBMSSOCN;Initial Catalog=DataBaseName;User ID=User;Password=Pass;

我错过了什么吗?在尝试连接之前,我还应该做点其他事情吗?

Am I missing something? Should I do something else, before I try to connect?

我认为我对Windows身份验证感到困惑,可以绕过它直接连接到SQL吗?

I think I have a mess with the windows authentification thing, can I bypass it and connect directly to the SQL?

注意在服务器上启用了远程连接,我检查了端口为1433。

Note that remote connection is enabled on the server, and I checked the port being 1433.

编辑:

我正在寻找找到这个问题,我意识到,当我远程连接到服务器时,我使用端口(190.xxx.xxx.xxx:yyyy)。我的问题就在这里,不是吗?

While I was looking for a solution I found this question and I realized that when I connect remotely to the server I use a port (190.xxx.xxx.xxx:yyyy). My problem is right there, isn't it?

编辑2:
我的解决方案:我使用的connectionString没问题,问题出在po rt。我为办公室IP打开了它,一切都像个魅力。

My solution: the connectionString I was using was alright, the problem was the port. I opened it for my office IP and everythings works like a charm.

推荐答案

我强烈建议您使用 SqlConnectionStringBuilder 类,该类使您可以通过属性配置连接字符串,并保证创建有效的连接字符串。

I strongly suggest you use the SqlConnectionStringBuilder class, which allows you to configure connection strings through properties and guarantees that valid connection strings are created.

按如下所示使用它:

SqlConnectionStringBuilder b = new SqlConnectionStringBuilder();
b.DataSource = "190.xxx.xxx.xxx\ServerName";
b.InitialCatalog = "DataBaseName";
b.IntegratedSecurity = false;
b.UserId = "...";
b.Password = "...";

string connectionString = b.ConnectionString;

这篇关于远程SQL Server的ConnectionString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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