Search code examples
sql-server-2008connection

Add login and connect to SQL with SQL Server Authentication


I want to add a user in SQL Server 2008 so I can use SQL Server Authentication instead Windows Authentication for connecting to SQL, and have tried this code to create a user with login:

CREATE login [newLog] with password = 'passnewLognewLog'

I get it done, but when I want to connect to SQL Server using SQL Authentication, I get this message

Cannot connect to "Mydb"
additional information: Login failed for user 'newLog'. (Microsoft SQL Server, Error: 18456 )

What am I missing here?


Solution

  • After creating the login, you need to add the user to the database. This example is from the sql server documentation for CREATE USER:

    CREATE LOGIN AbolrousHazem 
        WITH PASSWORD = '340$Uuxwp7Mcxo7Khy';
    USE AdventureWorks2008R2;
    CREATE USER AbolrousHazem FOR LOGIN AbolrousHazem;
    GO 
    

    Edit

    To test, I ran this T-SQL:

    create login Foo with password ='f00';
    go
    
    use TestDB
    create user Foo for Login Foo
    go
    

    and opened a connection successfully using this connection string:

    "Server=<server>; user id=Foo; password=f00; initial catalog=TestDB"