Create failed for database , SMO,C#

﹥>﹥吖頭↗ 提交于 2019-12-24 07:09:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!