MS SQL как начать?
От: Dimetrius Украина  
Дата: 14.06.06 13:57
Оценка:
Я только начинаю работать с MS SQL, так что, если глупый вопрос извиняйте... Поставил себе MS SQL 2005 и заодно Management Studio Express. C помощью Management Studio создаю новую базу в ней простую таблицу-элементарные запросы созданные с помощью той же Management Studio работают нормально. Теперь хочу организовать программный доступ к базе средствами ADO.NET. Создаю в студии(VS 2005) консольный проект и добавляю код:


using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;



namespace ConsoleApplicationSQLADO
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection conn = new SqlConnection("Server=localhost; Database=MyData; Integrated Security=true");
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM Customers",conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(reader["id"]+" "+ reader["Name"]);
            }
            conn.Close();
            Console.WriteLine("Hello!");
            Console.ReadLine();

        }

    }
}


На этапе открытия соединения( conn.Open()) выдает следующую ошибку:


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)


Говорит что по умолчанию сервер не позволяет открывать удаленные подключения...

Как это исправить?
Re: MS SQL как начать?
От: Dimetrius Украина  
Дата: 14.06.06 16:37
Оценка:
Неужели никто не хочет помочь?
Re[2]: MS SQL как начать?
От: Lloyd Россия  
Дата: 14.06.06 16:41
Оценка:
Здравствуйте, Dimetrius, Вы писали:

D>Неужели никто не хочет помочь?


В SQL Server Surface Area Configuration заходил?
Re[3]: MS SQL как начать?
От: Dimetrius Украина  
Дата: 14.06.06 16:54
Оценка:
Здравствуйте, Lloyd, Вы писали:

L>Здравствуйте, Dimetrius, Вы писали:


D>>Неужели никто не хочет помочь?


L>В SQL Server Surface Area Configuration заходил?


Смотрел! На что именно там нужно обратить внимание?
Re[4]: MS SQL как начать?
От: Zaks  
Дата: 14.06.06 16:57
Оценка:
To assign a TCP/IP port number to the SQL Server Database Engine
In SQL Server Configuration Manager, in the console pane, expand SQL Server 2005 Network Configuration, expand Protocols for <instance name>, and then double-click TCP/IP.

In the TCP/IP Properties dialog box, on the IP Addresses tab, several IP addresses appear, in the format IP1, IP2, up to IPAll. One of these are for the IP address of the loopback adapter, 127.0.0.1. Additional IP addresses appear for each IP Address on the computer. Right-click each address, and then click Properties to identify the IP address that you wish to configure.

If the TCP Dynamic Ports dialog box contains 0, indicating the Database Engine is listening on dynamic ports, delete the 0.

In the IPn Properties area box, in the TCP Port box, type the port number you wish this IP address to listen on, and then click OK.

In the console pane, click SQL Server 2005 Services.

In the details pane, right-click SQL Server (<instance name>) and then click restart, to stop and restart SQL Server.

After you have configured SQL Server to listen on a specific port there are three ways to connect to a specific port with a client application:

Run the SQL Server Browser service on the server to connect to the Database Engine instance by name.


Create an alias on the client, specifying the port number.


Program the client to connect using a custom connection string.
Re[5]: MS SQL как начать?
От: Dimetrius Украина  
Дата: 14.06.06 17:44
Оценка:
Здравствуйте, Zaks, Вы писали:

Z>To assign a TCP/IP port number to the SQL Server Database Engine

Z>In SQL Server Configuration Manager, in the console pane, expand SQL Server 2005 Network Configuration, expand Protocols for <instance name>, and then double-click TCP/IP.

Z>In the TCP/IP Properties dialog box, on the IP Addresses tab, several IP addresses appear, in the format IP1, IP2, up to IPAll. One of these are for the IP address of the loopback adapter, 127.0.0.1. Additional IP addresses appear for each IP Address on the computer. Right-click each address, and then click Properties to identify the IP address that you wish to configure.


Z>If the TCP Dynamic Ports dialog box contains 0, indicating the Database Engine is listening on dynamic ports, delete the 0.


Z>In the IPn Properties area box, in the TCP Port box, type the port number you wish this IP address to listen on, and then click OK.


Z>In the console pane, click SQL Server 2005 Services.


Z>In the details pane, right-click SQL Server (<instance name>) and then click restart, to stop and restart SQL Server.


Z>After you have configured SQL Server to listen on a specific port there are three ways to connect to a specific port with a client application:


это я сделал! не помогло... та же ошибка



Z>Program the client to connect using a custom connection string.

custom connection... это вроде то что я и делаю...
SqlConnection conn = new SqlConnection("Server=localhost; Database=MyData; Integrated Security=true");
Re[6]: MS SQL как начать?
От: DarkSid Россия  
Дата: 14.06.06 18:12
Оценка:
Здравствуйте, Dimetrius, Вы писали:

D>Здравствуйте, Zaks, Вы писали:


Z>>To assign a TCP/IP port number to the SQL Server Database Engine

Z>>In SQL Server Configuration Manager, in the console pane, expand SQL Server 2005 Network Configuration, expand Protocols for <instance name>, and then double-click TCP/IP.

Z>>In the TCP/IP Properties dialog box, on the IP Addresses tab, several IP addresses appear, in the format IP1, IP2, up to IPAll. One of these are for the IP address of the loopback adapter, 127.0.0.1. Additional IP addresses appear for each IP Address on the computer. Right-click each address, and then click Properties to identify the IP address that you wish to configure.


Z>>If the TCP Dynamic Ports dialog box contains 0, indicating the Database Engine is listening on dynamic ports, delete the 0.


Z>>In the IPn Properties area box, in the TCP Port box, type the port number you wish this IP address to listen on, and then click OK.


Z>>In the console pane, click SQL Server 2005 Services.


Z>>In the details pane, right-click SQL Server (<instance name>) and then click restart, to stop and restart SQL Server.


Z>>After you have configured SQL Server to listen on a specific port there are three ways to connect to a specific port with a client application:


D>это я сделал! не помогло... та же ошибка




Z>>Program the client to connect using a custom connection string.

D>custom connection... это вроде то что я и делаю...
D>
D>SqlConnection conn = new SqlConnection("Server=localhost; Database=MyData; Integrated Security=true");
D>



посмотри здесь http://www.connectionstrings.com/
Re[6]: MS SQL как начать?
От: Dimetrius Украина  
Дата: 14.06.06 19:40
Оценка:
Я в шоке... Будут еще какие-нибудь идеи?
Re[4]: MS SQL как начать?
От: _d_m_  
Дата: 14.06.06 23:56
Оценка:
Здравствуйте, Dimetrius, Вы писали:

D>Здравствуйте, Lloyd, Вы писали:


L>>Здравствуйте, Dimetrius, Вы писали:


D>>>Неужели никто не хочет помочь?


L>>В SQL Server Surface Area Configuration заходил?


D>Смотрел! На что именно там нужно обратить внимание?


Там надо включить remote connections, т.к. ты соединяешься по TCP — а это всегда считается удаленным соединением.
Если тебе не нужны удаленные соединения, то строка соединения должна выглядеть след. образом:

SqlConnection conn = new SqlConnection("Server=(local); Database=MyData; Integrated Security=true");


Как я понял, ты установил SQL Express, что необходимо было указать в своем посте. Соответственно, по умолчанию устанавливается именнованный инстанс зовется который sqlexpress. А значит строка соединения будет уже такой:

SqlConnection conn = new SqlConnection("Server=(local)\sqlexpress; Database=MyData; Integrated Security=true");
Re[5]: MS SQL как начать?
От: kon_v_palto  
Дата: 15.06.06 06:13
Оценка:
Здравствуйте, _d_m_, Вы писали:

___>Как я понял, ты установил SQL Express, что необходимо было указать в своем посте. Соответственно, по умолчанию устанавливается именнованный инстанс зовется который sqlexpress. А значит строка соединения будет уже такой:


___>
___>SqlConnection conn = new SqlConnection("Server=(local)\sqlexpress; Database=MyData; Integrated Security=true");
___>


А еще про етскейп последовательности не забываем.

SqlConnection conn = new SqlConnection("Server=(local)\\sqlexpress; Database=MyData; Integrated Security=true");
Re[5]: MS SQL как начать?
От: Dimetrius Украина  
Дата: 15.06.06 08:35
Оценка:
Здравствуйте, _d_m_, Вы писали:
___>Там надо включить remote connections, т.к. ты соединяешься по TCP — а это всегда считается удаленным соединением.
___>Если тебе не нужны удаленные соединения, то строка соединения должна выглядеть след. образом:

___>
___>SqlConnection conn = new SqlConnection("Server=(local); Database=MyData; Integrated Security=true");
___>


___>Как я понял, ты установил SQL Express, что необходимо было указать в своем посте. Соответственно, по умолчанию устанавливается именнованный инстанс зовется который sqlexpress. А значит строка соединения будет уже такой:


___>
___>SqlConnection conn = new SqlConnection("Server=(local)\sqlexpress; Database=MyData; Integrated Security=true");
___>


Спасибо большое, заработало)))
проблема была в "Server=(local)\sqlexpress"
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.