0

I am having problems connecting to a SQL Server database from C#.

The exception that returns is the login has failed for the specified user, which is clear enough. However, I am not sure why it fails as the username and password are definitely correct. Are there any settings I need to enable on the SQL Server to allow this to happen, as it is a default express install,

Thanks,

Below is my connection code if I'm missing anything obvious.

static void Main(string[] args) {
    try
    {
        SqlConnection con = new SqlConnection(@"Data Source = .\SQLEXPRESS;Initial Catalog=furniture_display;User ID=login;Password=login");
        con.Open();
        Console.WriteLine("all ok");
        con.Close();
    }
    catch (SqlException err)
    {
        Console.WriteLine(err);
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Oliver
  • 102
  • 1
  • 10
  • 1
    connect to sql server using the credentials you are passing in connection string i.e SQL Server UserID and the Password. via SSMS and execute `SELECT @@SERVERNAME` . what ever this query return us that value in your Data Source property of your connection string. – M.Ali Apr 04 '15 at 16:41
  • If that user is definitely set up with those credentials, this should work. Have you mapped your database login to the `furniture_display` database? And can you post the exact error message which is returned? – Rhumborl Apr 04 '15 at 16:45
  • Is that login and password a Windows user or aSQL Server user? – ErikEJ Apr 04 '15 at 17:16
  • @Oliver this may be trivial but have you tried removing the spaces for your datasource ? `Data Source=.\SQLEXPRESS` or try `Data Source=SERVERNAME\SQLEXPRESS` .Additionally if you have SSMS I would connect to the instance to verify – bumble_bee_tuna Apr 04 '15 at 20:47
  • Hi all, I had not correctly mapped (thought I had, sorry!) the server user to the database in the correct way and that stopped the login to the SSMS. Apologies and thank you all very much for your time and efforts. – Oliver Apr 05 '15 at 14:52

1 Answers1

0

According to your code Data Source = .\SQLEXPRESS, you'r trying to connect to a local server. If so you don't need any ID and Password. And be aware of using Catalog, it's somehow tricky and I hate it. To know how it's working, check this out.

Actually I'm using this code and it works like a charm:

SqlConnection con = new SqlConnection(@"Server = localhost; Database = furniture_display; Integrated Security=True;");
Community
  • 1
  • 1
Ali Torabi
  • 32
  • 1
  • 14