|
|
Visual Basic .NETSORGENTI - DATABASE - CREARE UN DATABASE SQL SERVER A RUNTIME Sub CreateDatabase(ByVal connString As String, ByVal dbName As String, Optional ByVal dropExisting As Boolean = True) Dim cn As New SqlConnection(connString) Try ' the command for creating the DB Dim cmdCreate As New SqlCommand("CREATE DATABASE [" & dbName & "]", cn) cn.Open() If dropExisting Then ' drop the existing DB with the same name Dim cmdDrop As New SqlCommand("IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'" & dbName & "') DROP DATABASE [" & dbName & "]", cn) cmdDrop.ExecuteNonQuery() End If ' create the DB cmdCreate.ExecuteNonQuery() Finally cn.Close() End Try End Sub |