Reputation: 115
I developed windows service and created installer. I logged in as admin. Now problem is with connection string. When I'm trying to connect with windows authentication its giving this error.."CREATE DATABASE permission denied in database 'master'." Below is my code. How to set permission in client system with c# code? is there any other way to do this.. please help,thank you
using (var conn = new SqlConnection("data source=.\\sqlexpress;integrated security=true"))
{
using (var cmd = new SqlCommand("CREATE DATABASE [db_namexx]", conn))
{
conn.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
}
}
Upvotes: 0
Views: 966
Reputation: 46390
The Windows service account needs to be a member of the SQL Server sysadmin role or the db_creator server role in order to create databases. Below is a sample script that you can tweak for your needs, and perhaps run it as part of the installer for your service. The account running this script must be provisioned as a sysadmin role member.
CREATE LOGIN [NT AUTHORITY\LOCAL SERVICE] FROM WINDOWS;
EXEC sp_addsrvrolemember 'NT AUTHORITY\LOCAL SERVICE', 'sysadmin';
Upvotes: 1