Function CreateDatabase(ByVal sDBName As String, ByVal sPath As String, Optional ByVal sDataSize As String = "2", Optional ByVal sMaxDataSize As String = "10", Optional ByVal sLogSize As String = "1", Optional ByVal sMaxLogSize As String = "5", Optional ByVal sGrowth As String = "10", Optional ByVal auto_shrink As Boolean = False, Optional ByVal auto_update_statistics As Boolean = True, Optional ByVal auto_close As Boolean = False, Optional ByVal auto_create_statistics As Boolean = True) As Boolean
Dim str As String
Dim db_option1 As String = "sp_dboption ‘" & sDBName & "‘, autoshrink, true"
Dim db_option2 As String = "sp_dboption ‘" & sDBName & "‘, ‘auto update statistics’, false"
Try
Select Case guInternal.DBTypeSelected
Case enumDBTYPE.MSSQLServer
‘str = "CREATE DATABASE MyDatabase ON PRIMARY (NAME = MyDatabase_Data, FILENAME = ‘C:\\MyDatabaseData.mdf’, SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) LOG ON (NAME = MyDatabase_Log, FILENAME = ‘C:\\MyDatabaseLog.ldf’, SIZE = 1MB, MAXSIZE = 5MB, FILEGROWTH = 10%)"
str = "CREATE DATABASE " & sDBName & " ON PRIMARY (NAME = " & sDBName & "_Data, FILENAME = ‘" & sPath & sDBName & "Data.mdf’, SIZE = " & sDataSize & "MB, MAXSIZE = " & sMaxDataSize & "MB, FILEGROWTH = " & sGrowth & "%) LOG ON (NAME = " & sDBName & "_Log, FILENAME = ‘" & sPath & sDBName & "Log.ldf’, SIZE = " & sLogSize & "MB, MAXSIZE = " & sMaxLogSize & "MB, FILEGROWTH = " & sGrowth & "%)"
Dim objDB As New clsCMDBObject
With objDB
‘ Specific parameters for execution
.ExecuteAsync = False
.DataTypeReturn = clsCMDBObject.enumDB_OBJECT_RETURN_TYPE.NoData
‘Command object definition
.Command.CommandType = CommandType.Text
.Command.CommandText = str
If ExecuteDBObject(objDB) = True Then
‘ Execution done without errors
‘Console.WriteLine("DataBase is Created Successfully")
‘objCMDataTable.LoadFromDataTable(objDB.DataTable(0))
End If
objDB.Dispose()
End With
objDB = Nothing
If auto_shrink <> False Then
objDB = New clsCMDBObject
With objDB
‘ Specific parameters for execution
.ExecuteAsync = False
.DataTypeReturn = clsCMDBObject.enumDB_OBJECT_RETURN_TYPE.NoData
‘Command object definition
.Command.CommandType = CommandType.Text
.Command.CommandText = db_option1
If ExecuteDBObject(objDB) = True Then
‘ Execution done without errors
End If
objDB.Dispose()
End With
objDB = Nothing
End If
If auto_update_statistics <> True Then
objDB = New clsCMDBObject
With objDB
‘ Specific parameters for execution
.ExecuteAsync = False
.DataTypeReturn = clsCMDBObject.enumDB_OBJECT_RETURN_TYPE.NoData
‘Command object definition
.Command.CommandType = CommandType.Text
.Command.CommandText = db_option2
If ExecuteDBObject(objDB) = True Then
‘ Execution done without errors
End If
objDB.Dispose()
End With
objDB = Nothing
End If

CreateDatabase = True


Case enumDBTYPE.Oracle
Case enumDBTYPE.DB2
Case enumDBTYPE.AS400
Case 0 ‘in this case the method "openDatabase" is not called, so the DBType is not selected
Throw New ApplicationException("Please call ""OpenDatabase"" first to create an active connection.")
End Select
Catch ex As Exception
CreateDatabase = False
‘MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information)
If ex.Message = "Database ‘" & sDBName & "‘ already exists." Then
Call CatchError("The database being created, exists on the current server.")
ElseIf ex.Message = "The ConnectionString property has not been initialized." Then
Call CatchError("Trying to create a database on a disconnected server.")
ElseIf guInternal.DBTypeSelected = 0 Then
Console.WriteLine(ex.Message & " ::ex.Message")
Call CatchError(ex.Message)
End If
End Try
End Function
