C# 使用命名实例连接到 SQL 服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/307014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:31:32  来源:igfitidea点击:

C# Connecting to a SQL server with a named instance?

c#sql

提问by

A client of mine has told me the program I made for them won't connect to a SQL server named instance, I have a standard SQL server with no named instance so I'm wondering how I can test this. A named instance connection string look like the one below, could the backslash be were my code fails?

我的一个客户告诉我,我为他们制作的程序无法连接到名为实例的 SQL 服务器,我有一个没有命名实例的标准 SQL 服务器,所以我想知道如何测试它。命名实例连接字符串如下所示,反斜杠可能是我的代码失败吗?

Driver={SQL Native Client};Server=myServerName\theInstanceName;Database=myDataBase;

Driver={SQL Native Client};Server=myServerName\theInstanceName;Database=myDataBase;

My code is as follows:

我的代码如下:

sqlServer=s.Substring(keyword.Length,s.Length-keyword.Length);
FormODBC formODBC=new FormODBC(this);
formODBC.SetSqlServer(sqlServer,dbUsername,dbPassword,database,table);
formODBC.ReadData();

How should I handle the backslash as I suspect this may be the problem?

我应该如何处理反斜杠,因为我怀疑这可能是问题所在?

Thanks

谢谢

回答by

Scott, At the risk of stating the obvious, have you tried setting up a named instance in your own development environment? I don't actually know the answer to your question but I've never personally run into a solution where testing the scenario that is failing directly didn't help. At a minimum you ought to be able to get better debugging information as to precisely what is failing. Good luck getting this resolved.

Scott,冒着显而易见的风险,您是否尝试过在自己的开发环境中设置命名实例?我实际上不知道您的问题的答案,但我个人从未遇到过测试直接失败的场景没有帮助的解决方案。至少,您应该能够获得更好的调试信息,以准确了解失败的原因。祝你好运解决这个问题。

Regards, Chris

问候, 克里斯

回答by Paul Kapustin

We have SQL servers with named instances. Examples: myservername\sql2005. Backslash is fine, in the conection string server name will be "myservername\sql2005", works 100% fine. You can have a "regular instance" on the same server, will be "myservername"

我们有带有命名实例的 SQL 服务器。示例:myservername\sql2005。反斜杠很好,在连接字符串服务器名称中将是“myservername\sql2005”,100% 正常工作。你可以在同一台服务器上有一个“常规实例”,将是“myservername”

PS just unit test your function making connection string returns "myservername\sql2005".

PS 只是对您的函数进行单元测试,使连接字符串返回“myservername\sql2005”。

回答by JB King

There is also the occassional SQL Express edition case where the name is MyServerName\SQLEXPRESS. This can trip up some people because they wouldn't think to specify the server that way.

还有偶尔的 SQL Express 版本案例,其中名称为 MyServerName\SQLEXPRESS。这可能会绊倒一些人,因为他们不会想到以这种方式指定服务器。

回答by flesh

You can extract your connection string to a config file (note this isn't necessarily secure - that will depend on how their SQL security server is set up and the account your application is running under) - then your client can just add the appropriate connection string for their server to your application's config when deployed.

您可以将连接字符串提取到配置文件中(请注意,这不一定是安全的 - 这取决于他们的 SQL 安全服务器的设置方式以及您的应用程序在其下运行的帐户) - 然后您的客户端可以添加适当的连接部署时将其服务器的字符串添加到应用程序的配置中。

Notes:

笔记:

  • You can create a connection string by renaming a .txt file to a .udl file and running through until you can connect to your/their server.
  • Your unamed instance can actually be accessed by name - the machine name on which the SQL server is installed
  • 您可以通过将 .txt 文件重命名为 .udl 文件并运行直到可以连接到您/他们的服务器来创建连接字符串。
  • 您的未命名实例实际上可以通过名称访问 - 安装 SQL 服务器的机器名称

回答by RedFilter

Also, remember that backslash is an escape character in C# strings. If your instance name is contained in a string variable, make sure you do either:

另外,请记住反斜杠是 C# 字符串中的转义字符。如果您的实例名称包含在字符串变量中,请确保您执行以下任一操作:

string server = "myServer\myInstance";

or

或者

string server = @"myServer\myInstance";

回答by Neil Barnwell

The answer is to ensure your connection string is configurable, and set it to something like:

答案是确保您的连接字符串是可配置的,并将其设置为:

data source=localhost\msexpress;database=dbname;trusted_connection=true;

数据源=localhost\msexpress;数据库=dbname;trusted_connection=true;