简体   繁体   English

C#使用命名实例连接到SQL Server吗?

[英]C# Connecting to a SQL server with a named instance?

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. 我的一个客户告诉我,我为他们制作的程序无法连接到名为实例的SQL Server,我有一个没有命名实例的标准SQL Server,所以我想知道如何进行测试。 A named instance connection string look like the one below, could the backslash be were my code fails? 一个命名实例连接字符串看起来像下面的字符串,反斜杠可能是我的代码失败吗?

Driver={SQL Native Client};Server=myServerName\\theInstanceName;Database=myDataBase; 驱动程序= {SQL Native Client};服务器= myServerName \\ theInstanceName;数据库= 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 谢谢

We have SQL servers with named instances. 我们有带有命名实例的SQL服务器。 Examples: myservername\\sql2005. 示例:myservername \\ sql2005。 Backslash is fine, in the conection string server name will be "myservername\\sql2005", works 100% fine. 反斜杠很好,在连接字符串服务器名称中将是“ myservername \\ sql2005”,可以100%正常工作。 You can have a "regular instance" on the same server, will be "myservername" 您可以在同一服务器上有一个“常规实例”,将是“ myservername”

PS just unit test your function making connection string returns "myservername\\sql2005". PS只是对您的函数进行单元测试,使连接字符串返回“ myservername \\ sql2005”。

Also, remember that backslash is an escape character in C# strings. 另外,请记住,反斜杠是C#字符串中的转义字符。 If your instance name is contained in a string variable, make sure you do either: 如果您的实例名称包含在字符串变量中,请确保执行以下任一操作:

string server = "myServer\\myInstance";

or 要么

string server = @"myServer\myInstance";

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;

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. 祝你好运得到解决。

Regards, Chris 克里斯,问候

There is also the occassional SQL Express edition case where the name is MyServerName\\SQLEXPRESS. 在偶尔的SQL Express版本中,名称为MyServerName \\ SQLEXPRESS。 This can trip up some people because they wouldn't think to specify the server that way. 这会使某些人不满意,因为他们不会以这种方式指定服务器。

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. 您可以通过将.txt文件重命名为.udl文件并运行直到可以连接到您的服务器来创建连接字符串。
  • Your unamed instance can actually be accessed by name - the machine name on which the SQL server is installed 可以通过名称实际访问您的已取消实例的实例-安装SQL Server的计算机名称

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

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