问题
I am trying to create a new database programmatically using SMO (c#).
I am working on SQL Server 2005. This is the code :
ServerConnection connection = new ServerConnection(serverName, userName, password);
Server sqlServer = new Server(connection);
Database newDB = new Database(sqlServer, databaseName);
newDB.Create();
I get the exception :
"Create failed for database."
What could be the problem and how can I create a new DB? thanks...
回答1:
I tried this: with a sysadmin user, everything works just fine. With a "normal" user, I get the same error as you do.
Try this:
ServerConnection connection = new ServerConnection(serverName, userName, password);
Server sqlServer = new Server(connection);
Database newDB = new Database(sqlServer, databaseName);
try
{
newDB.Create();
}
catch(Exception exc)
{
string msg1 = exc.Message;
if(exc.InnerException != null)
{
string msg2 = exc.InnerException.Message;
if(exc.InnerException.InnerException != null)
{
string msg3 = exc.InnerException.InnerExceptionMessage;
}
}
}
Do you see a msg2
and a msg3
?? What do they say???
When using a user without the necessary permission, my msg3
is very clear:
CREATE DATABASE permission denied in database 'master'.
来源:https://stackoverflow.com/questions/7783229/create-failed-for-database-smo-c