11 Replies - 1531 Views - Last Post: 06 July 2010 - 08:37 AM Rate Topic: -----

#1 hamidkhl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 03-November 08

Connected Connection to SQL Server

Posted 05 July 2010 - 09:45 PM

Hi every body

I wanna connect to SQL Server 2005 (Connected Architecture )
It's my code:

class PersonManager
    {
        SqlConnection MyConnection = new SqlConnection();
        SqlCommand MyCommand = new SqlCommand();
        public List<Person> SelectAll()
        {
            MyConnection.ConnectionString = "Server=localhost;Database=ApartmentManeger;integrated security=true";//ApartmentManager.Properties.Settings.Default.MySqlConnection;
            MyCommand.Connection = MyConnection;
            MyCommand.CommandType= System.Data.CommandType.Text;
            MyCommand.CommandText = "Select FirstName, LastName, Gender From Person";
            MyCommand.Connection.Open();
            MyCommand.ExecuteReader();
            SqlDataReader MyReader = MyCommand.ExecuteReader(); ;
            List<Person> MyList=new List<Person>();
            while(MyReader.Read())
            {
                
                Person p=new Person();
                p.Id = MyReader.GetString(MyReader.GetOrdinal("ID"));
                MyList.Add(p);
            }
            MyCommand.Connection.Close();
            return MyList;
            
            
        }



But an error occurs, this is the erroe:

An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

is it something wrong with my SQL Server?
please help me

This post has been edited by hamidkhl: 05 July 2010 - 09:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Connected Connection to SQL Server

#2 mayfan77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-June 10

Re: Connected Connection to SQL Server

Posted 05 July 2010 - 10:00 PM

Did you give username and password to ur database?
Was This Post Helpful? 0
  • +
  • -

#3 hamidkhl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 03-November 08

Re: Connected Connection to SQL Server

Posted 05 July 2010 - 10:38 PM

No, I did not, and integrated security is true, I think I should change some settings on sql, but I don't know where and how?!

This post has been edited by hamidkhl: 05 July 2010 - 10:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 Bacanze  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 202
  • Joined: 09-April 10

Re: Connected Connection to SQL Server

Posted 06 July 2010 - 03:49 AM

Okay I had a little trick for getting correct connection strings, hopefully it's applicable to you.


Go

Server Explore > right click Data Connetions > Add Connection... > Select Server Name > Select or Enter Database Name

Now test the connection using the appropriate button on the window, then click the Advanced... button and look at the bottom of the new window that's appeared, there should be a connection string there, simply copy and paste and your good to go :bananaman:

Edit: probably better if you store your string as a const at class level, or store it within the applications Settings.

This post has been edited by Bacanze: 06 July 2010 - 03:52 AM

Was This Post Helpful? 0
  • +
  • -

#5 mayfan77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-June 10

Re: Connected Connection to SQL Server

Posted 06 July 2010 - 04:32 AM

Yeah. Bacanze is right. I have done that before. The best way to get the right connection string. Thanks Bacanze.
Was This Post Helpful? 0
  • +
  • -

#6 hamidkhl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 03-November 08

Re: Connected Connection to SQL Server

Posted 06 July 2010 - 05:32 AM

thank you Bacanze, I tried your way
but when I want test connection occurs same error again, I studied some where that it's about .Net and I should repair it, but it does not work
my SQL Server is 2005, and I use VS2005 do you think by VS2008 this will occur again?

This post has been edited by hamidkhl: 06 July 2010 - 05:34 AM

Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Connected Connection to SQL Server

Posted 06 July 2010 - 05:38 AM

In SQL Server Management Studio, right-click on the server instance and go to Properties. You will see a "Connections" tab on the left side of the dialog box. On that tab, you will see a checkbox for "Allow remote connections to this server".
Was This Post Helpful? 0
  • +
  • -

#8 hamidkhl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 03-November 08

Re: Connected Connection to SQL Server

Posted 06 July 2010 - 05:51 AM

excuse me I didn't find server instances :innocent:
Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Connected Connection to SQL Server

Posted 06 July 2010 - 06:11 AM

Are you using SQL Server Management Studio? If so, your server instance is what you are connecting to. It's the top line in the "Object Explorer"(hit F8 if the Object Explorer is not showing).
Was This Post Helpful? 0
  • +
  • -

#10 hamidkhl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 03-November 08

Re: Connected Connection to SQL Server

Posted 06 July 2010 - 07:16 AM

check box was true
but problem does not solved

I think It's about my .net because when I go : start> Microsoft SQL Server 2005> Configuration Tools> SQL Server Surface Area Configuration
I see this:

Unable to fine a version of runtime to run this application
Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Connected Connection to SQL Server

Posted 06 July 2010 - 08:25 AM

try to open SQL Server Configuration Manager. Make sure your's matches this...

Attached Image
Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Connected Connection to SQL Server

Posted 06 July 2010 - 08:37 AM

Read through this article. That's a pretty common error being received and that article goes through a few things you can do :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1