I've solved the task.
The new function code:
Imports System.Reflection
Imports System.Data.Common
Friend Function CreateDatabase(ByVal factory As DbProviderFactory, ByVal connectionString As String) As Boolean
Dim asm As Assembly = Assembly.Load(factory.GetType.Assembly.FullName)
Dim t As Type = asm.GetType("System.Data.SqlServerCe.SqlCeEngine")
Dim engine = Nothing
Try
engine = Activator.CreateInstance(t)
engine.LocalConnectionString = connectionString
engine.CreateDatabase()
Return True
Catch ex As SystemException
MsgBox(ex.Message, MsgBoxStyle.Critical)
Return False
Finally
If engine IsNot Nothing Then engine.Dispose()
End Try
End Function
Usage:
Dim factory As DbProviderFactory
Dim connectionString As String
factory = DbProviderFactories.GetFactory("System.Data.SqlServerCe.4.0")
connectionString = "Data Source=test40.sdf;"
CreateDatabase(factory, connectionString)
factory = DbProviderFactories.GetFactory("System.Data.SqlServerCe.3.5")
connectionString = "Data Source=test35.sdf;"
CreateDatabase(factory, connectionString)