Touble连接到SQL服务器。 [英] Touble connecting to a SQL server.

查看:80
本文介绍了Touble连接到SQL服务器。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在visual studio vb.net中编写了两行非常基本的代码来连接到一个在Visual Studio的一个实例中工作正常的SQL服务器,但在复制到另一个实例时不起作用。我已经检查过两个实例中都包含了所有必需的引用。使问题进一步复杂化的是我可以在两个实例中连接到不同的SQL服务器,但是我只能在一个实例中连接到我想要的服务器。有没有人想过会导致这种行为的Visual Studio实例之间会有什么不同?



我得到的错误是服务器不能是找到。连接字符串在所有情况下都是相同的



 Dim con As New SqlConnection(Data Source = server1; Initial Catalog = somedatabase; User ID = myid;密码= mypassword;加密= False; TrustServerCertificate = False)
con.Open()





我感谢任何想法。这让我陷入困境。



我尝试过的事情:



我试过将相同的代码复制到其他现有的程序,它工作得很好。当复制到新程序时,它会失败。

解决方案

这是一个完美的例子,说明你为什么不对连接字符串进行硬编码!

您需要提供一个连接字符串,其中包含对SQL服务器的完整引用,通常类似于COMPUTERNAME \ _SQLINSTANCE NAME。

例如,我的一个是: />

 DataSource = GRIFF-DESKTOP \SQLExpress;初始目录= ... 

您通常只能使用本地数据库中的实例名称



您还需要检查SQL Server实例是否配置为允许远程连接,因为出于安全原因,这通常是禁用的。


< blockquote>请阅读: SQL Server连接字符串 - ConnectionStrings.com [ ^ ]


I have two lines of very basic code written in visual studio vb.net to connect to a SQL server that works fine in one instance of visual studio, but does not work when copied over to another instance. I have checked that all the necessary references are included in both instances. Further complicating the issue is that I can connect to a different SQL server in both instances, but I can connect to the server I want in only one instance. Does anyone have a thought as to what may be different between the instances of Visual Studio that would cause this behavior?

The error I am getting is that the server cannot be found. The connection string is identical in all cases

 Dim con As New SqlConnection("Data Source=server1;Initial Catalog=somedatabase;User ID=myid;Password=mypassword;Encrypt=False;TrustServerCertificate=False")
con.Open()



I appreciate any ideas. This is throwing me for a loop.

What I have tried:

I have tried copying the same code to other existing programs and it works just fine. When copied to a new program, it fails.

解决方案

This is a perfect example of why you don't hardcode connection strings!
You need to provide a connection string with a full reference to teh instenace of SQL server, which normally will be something like "COMPUTERNAME\SQLINSTANCE NAME".
For example, one of mine is:

DataSource=GRIFF-DESKTOP\SQLExpress;Initial Catalog=...

You can only normally get away with just the instance name on local DB's

You also need to check that the SQL Server instance is configured to allow remote connections as this is often disabled for security reasons.


Please, read this: SQL Server connection strings - ConnectionStrings.com[^]


这篇关于Touble连接到SQL服务器。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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