“CREATE DATABASE permission denied in database ‘master’. (.Net SqlClient Data Provider)”
Oh SQL Server Express, how I hate you sometimes. Not only did you not let me specify an sa password when I installed you, but you didn’t take the time to make me a sysadmin either.
The worst part was that I got this when I was trying to get to a geek party an hour away from my house. Oh well. Part of the problem was that there was no clear answer (with example!) of how to get around this issue… so… here you go. 😉
Prerequisites for this technique:
- You have to be a local administrator on the box that has the SQL Server instance you’re trying add a database to.
- You have to be able to change the settings on Services on that box.
- You have to be able to start and stop Services on that box.
- Go to Services and Stop the service that is the target SQL Server instance. Also stop any SQL Server Agent service which are tied to that SQL Server instance service.
- Go to the Properties of the target SQL Server Instance service.
- In the Start Parameters field, add “;-m” to the *end* of whatever is already there. DON’T put in spaces OR include the quotes, and if nothing’s already there then you don’t need the semi-colon.
- Click OK and start the target SQL Service Instance service.
- Do a Windows Search for sqlcmd.exe … don’t be alarmed if there are several. You should use the one in the path that matches the SQL Server version that the target SQL Server Instance.
- Open an elevated command prompt.
- Path to the folder that has the sqlcmd executable in it. In my case, this was C:\Program Files\Microsoft SQL Server\110\Tools\Binn>.
- Run the following, with the appropriate user info, and DO include the quotes:
sqlcmd -S MachineName\SQLServerInstance -e -Q “EXEC sp_addsrvrolemember ‘UserDomain\UserName‘, ‘sysadmin’;”
- Go back to the service, remove the “;-m” you added, then restart the service.
- Verify that the user now has the sysadmin Server Role (via SSMS or however you want to do this).
- Don’t forget to restart any SQL Server Agent services that were stopped in #1 above.
- Enjoy. 🙂
Special thanks to Raul Garcia for putting me on the right path. You can see his original post here: http://blogs.msdn.com/b/raulga/archive/2007/07/12/disaster-recovery-what-to-do-when-the-sa-account-password-is-lost-in-sql-server-2005.aspx