Can't immediately connect to newly-created SQL Server database

后端 未结 4 1653
傲寒
傲寒 2021-01-12 00:25

I\'m creating a database using SQL Server Management Objects.

I wrote the following method to generate a database:

public static void CreateClientDat         


        
4条回答
  •  清酒与你
    2021-01-12 00:45

    You have to approach the database creation a little bit different. The code below handles the database creation correctly. Try it ;)

    private void CreateDatabase(string databaseName)
    {
    
        Microsoft.SqlServer.Management.Smo.Server sqlServer = 
                new Microsoft.SqlServer.Management.Smo.Server(_connection);
    
        Database createdDatabase = new Database();
        createdDatabase.Name = databaseName;
        createdDatabase.Parent = sqlServer;
        createdDatabase.Create();
    
    }
    

    In the code below, the _connection is a

    Microsoft.SqlServer.Management.Smo.ServerConnection
    object by the way.

    [Edit] Modified the code so the exception is not swallowed anymore.

提交回复
热议问题