2
votes

I create a database using ADO.NET SQL queries like this:

create database mydatabase
create login 'loginname' with password='somepassword'
create user 'username' for login loginname

The queries aren't exact, I've typed them as far as I could remember them. After that I don't see the database in my ms sql server 2008 r2 express management studio. I also can't log in with the newly created login. The "enable server authentication" is set to true. What should I do to see that database in the manager? Why can't I log in with newly created login? I've looked into the logs and it says that the password is incorrect.

I do have 2 sql express instances, but I use the same one when I run my sql select @@servername when run in management studio returns "BOGDAN". ADO.NET query returns "BOGDAN\BBF17ECB-69FF-4B" . Code is below: SqlConnection con = new SqlConnection("Data Source=BOGDAN;Integrated Security=True;User Instance=True"); SqlCommand cmd = new SqlCommand("select @@servername", con); con.Open(); string s = (string)cmd.ExecuteScalar(); Console.WriteLine("Server name:" + s); con.Close(); Console.ReadKey(false); Don't know where BBF17ECB-69FF-4B came from, I explicitly stated server name as "BOGDAN".

the script is: IF DB_ID('MyDatabase') IS NULL CREATE DATABASE [MyDatabase] USE MyDatabase IF NOT EXISTS(SELECT * FROM master.dbo.syslogins WHERE loginname = 'AUsername') CREATE LOGIN AUsername WITH PASSWORD='APassword' IF NOT EXISTS (SELECT * FROM sys.sysusers WHERE name='AUsername') CREATE USER AUsername FOR LOGIN AUsername EXEC sp_addrolemember N'db_owner', N'AUsername'

2
That's definitely a server configuration issue. I've tested exactly same application that runs sql scripts and after that the database is visible in the query window. Anybody has any ideas of what might be wrong? Regarding the user: I think the scripts for creating user are wrong.Bogdan Verbenets

2 Answers

0
votes

Do you have multiple instances of Sql Server on your box? Maybe an express install, and a developer edition install? It's possible you were pointed at the other instance when you created the db...I have done this before.

Are you sure that the database creation completed without error? Maybe it failed, and the db was never created.

Unfortunately, the details of the query that you would have run are where we would find the evidence for the problem.

0
votes

The problem was with the connection string: I had to remove "User Instance=true" from it. That fixed the problem! I thought the problem was with how I configurated the servers or with the SQL requests.