C#,SQL Server-连接到本地服务器而不指定数据源 [英] C#, SQL Server - Connecting to local server without specifying data source
问题描述
我正在编写一个C#应用程序。我连接到后端SQL Server进行一些处理。但是我今天遇到了一个奇怪的问题。这是我的代码:
I am writing a c# application. I connect to a backend SQL Server for some processing. However I hit into a weird issue today. This is my code:
static void Main(string[] args)
{
SqlConnection con = new SqlConnection("Integrated Security=true");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT SERVERPROPERTY('ProductVersion')", con);
cmd.CommandType = CommandType.Text;
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
Console.WriteLine(reader.GetString(0));
Console.Read();
}
我没有在连接字符串中指定服务器名称,但仍然连接到我的本地数据库并检索所需的数据。我想知道如何。默认数据源是否设置为本地计算机?
I haven’t given the server name in the Connection String, but still it connects to my local database and retrieves the required data. I am wondering how. Is the default data source set to local machine? Does "Integrated Security=true" have something to do with this?
有人可以解释吗?
谢谢,
Sornakumar S
Thanks, Sornakumar S
推荐答案
查看MSDN上ConnectionString上的文档,您会找到此信息
Looking at the docs on ConnectionString on MSDN you could find this information
要连接到本地计算机,请为服务器指定(local)。如果未指定
服务器名称,则将尝试连接到本地计算机上的
默认实例。
To connect to a local computer, specify "(local)" for the server. If a server name is not specified, a connection will be attempted to the default instance on the local computer.
这篇关于C#,SQL Server-连接到本地服务器而不指定数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!