0

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

7
  • What is the exception exactly? On which line? Commented Jul 17, 2014 at 6:49
  • on con.Open exception Login failed for user 'PLAYROOM\dhaval.patel'. InnerException null Commented Jul 17, 2014 at 6:50
  • Why you have defined Data source = . two times in connection string ? Commented Jul 17, 2014 at 6:51
  • I have chananged it still it's not working Commented Jul 17, 2014 at 6:56
  • Check if the server authentication mode is not set only to SQL Server Authentication. Commented Jul 17, 2014 at 7:07

2 Answers 2

1

Try to Put Integrated Security = true like this

con =  new SqlConnection("Data Source=.;Database=Master; Integrated Security=true");
Sign up to request clarification or add additional context in comments.

1 Comment

what is your sqlserver instance name ?
0

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"

3 Comments

yes if I used the con = new SqlConnection("Data Source=.;Database=Master;data source=.; uid=" + UserName + "; pwd=" + Password + ";"); then it's working fine
using SQL Authetication it's working fine but I want to open using Windows Authetication without any username and password
see this if kai help male to msdn.microsoft.com/en-us/library/ff647396.aspx @DhavalPatel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.