Cant connect to SQL DB via ConnectionString but can via ssms [duplicate]

479
07 сентября 2017, 15:51

This question already has an answer here:

  • Cannot open database “test” requested by the login. The login failed. Login failed for user 'xyz\ASPNET' 15 answers

There are and remote sql server and local C# app.

I can connect to DB via ssms The command '"C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\Ssms.exe" -S server -U user -P password ' performs great

But in code

my ConnectionString:

connectionString="Data Source=Server;Initial Catalog=DB;integrated security=false;User ID=user; Password=password;" providerName="System.Data.SqlClient"

when I try to open connction

        using (var conn = new SqlConnection(_connectionString))
        {
            conn.Open();

I get an error "Cannot open database "DB" requested by the login. The login failed. Login failed for user 'user'"

How can it possible? I have managed to connect by using the same user&pass to this server via ssms!! What other permission should have sql-user to connect to DB via C#-application?

P.S The point is (99.9%) on DB server side. And some permissions on it. Because with another remote Server everything works ok.

Answer 1

By default SSMS connects to the database Master, but your App try to connect to the specific Database. Perhaps you haven't sufficient rights for connection to specified Database. Try to connect to server using SSMS with following command:

"C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\Ssms.exe" -S server -d DB -U user -P password

Where the DB is your database name from connection string.

READ ALSO
как принимать предложения обмена в стим (оффлайн трейды) C#

как принимать предложения обмена в стим (оффлайн трейды) C#

Начал работать из стим API, могу получить полный список трейдов (входящих и тех что от меня), но вот как принимать их разобраться не могуНашел...

308
DataGridComboBoxColumn с разным набором items

DataGridComboBoxColumn с разным набором items

Имеется DataGrid, в нем несколько колонок и 1 DataGridComboBoxColumn, хотелось бы для каждого DataGridComboBoxColumn видеть различный набор элементов Xaml

292
Экспорт объектов в Excel C# через буфер обмена

Экспорт объектов в Excel C# через буфер обмена

Нужно скопировать содержимое List через буфер обмена в таблицу ExcelПройдёт ли преобразование List в Datatable с последующей вставкой в таблицу? Или...

375
Публикация приложения в C#

Публикация приложения в C#

Есть проект на C# в Visual Studio 2017

426