简体   繁体   English

如何在C#中使用Windows身份验证获取SQL Server名称

[英]How to get SQL Server Name Using Windows Authetication in C#

I am trying to fetch the Server Name using C# for that I am trying blow mentioned code. 我正在尝试使用C#来获取服务器名,因为我正在尝试提及代码。

    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader dr;

 con = new SqlConnection("Data Source=.;Database=Master;Integrated Security=SSPI");
                con.Open();
                cmd = new SqlCommand("select *  from sysservers  where srvproduct='SQL Server'", con);

                dr = cmd.ExecuteReader();

                while (dr.Read())
                {

                    ServerCollection.Add(dr[2].ToString());

                }

                dr.Close();

it's give me Exception like Login faild for user Dhaval.patel so can anyone please help how to connect using window's Authetication in C#. 这给了我一个异常,例如用户Dhaval.patel的登录失败,因此任何人都可以帮助使用C#中的window Authetication进行连接。

尝试这样使Integrated Security = true

con =  new SqlConnection("Data Source=.;Database=Master; Integrated Security=true");

It should be like :- 它应该像:-

 ("Server= localhost; Database=Master;Integrated Security=SSPI, Integrated Security=True");

If you have a named instance of SQL Server, you'll need to add that as well, eg, 如果您有SQL Server的命名实例,则还需要添加它,例如,

"Server=localhost\sqlexpress"

暂无
暂无

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

相关问题 如何使用c#从SQL语句中获取字段名称 - How to get field name from a SQL statement using c# 如何使用C#从SQL Server连接字符串或IIS应用程序名称获取应用程序名称? - How do I get the Application Name from either SQL Server connection string or the IIS application name using C#? 如何在C#中获取Windows登录名 - how to get windows login name in c# 如何使用C#从SQL Server获取电子邮件 - How to get an email from SQL Server using C# 如何使用 SQL Server 中的 Linq 获取不同的记录名称及其每个记录计数以绑定到 C# 中的 gridview - How to get the distinct record name and its each record count to bind to gridview in C# using Linq from SQL Server 如何使用C#获取可用的SQL Server实例(LAN)和版本(如“ Enterprise Edition(64位)”)名称的列表 - How to get the list of available sql server instance(in LAN) and edition(like “Enterprise Edition (64-bit)”) name using C# 如何使用C#或C / C ++在Windows 7中获取智能卡阅读器名称? - How to get smartcard reader name in Windows 7 using C# or C/C++? 如何在C#Windows Form应用程序中获取SQL Server中今天插入的数据的记录 - How to get record of today's inserted data in SQL Server in C# windows form application 如何在C#代码中的sql server,access和oracle中获取所有表的名称? - how to get all Table's name in sql server ,access and oracle in c# code? 使用 C# 在 Windows 10 中获取当前活动的应用程序名称 - Get current active application name in Windows 10 using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM