Dhaval Patel
Dhaval Patel

Reputation: 7591

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.

    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#.

Upvotes: 0

Views: 101

Answers (2)

Neel
Neel

Reputation: 11721

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, e.g.,

"Server=localhost\sqlexpress"

Upvotes: 0

Rajeev Kumar
Rajeev Kumar

Reputation: 4963

Try to Put Integrated Security = true like this

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

Upvotes: 1

Related Questions