Option ExplicitDim oSQLServer As New SQLDMO.SQLServer Private Sub cmdCreateDB_Click()
Dim oDatabase As New SQLDMO.Database Dim oDBFile As New SQLDMO.DBFile Dim oLogFile As New SQLDMO.LogFile
Dim sDatabaseName As String Dim sDBFilePath As String
On Error GoTo CreateDB_Err
'Input the database name sDatabaseName = InputBox("Enter the new database name", _ "New Database")
' Make sure that the database name isn't already used On Error Resume Next If oSQLServer.Databases(sDatabaseName).Name = "" Then End If
If Err = 0 Then MsgBox sDatabaseName & " already exists!" GoTo CreateDB_Exit Else On Error GoTo CreateDB_Err End If
'Set the object's database name oDatabase.Name = sDatabaseName
'Input the path for the data and log files sDBFilePath = InputBox("Enter the path for data and log files", _ "New Database", _ "c:\Program Files\Microsoft SQL Server\MSSQL\Data\" _ & sDatabaseName)
'Set the properties of the DBFile object oDBFile.Name = sDatabaseName & ".mdf" oDBFile.PhysicalName = sDBFilePath & ".mdf"
'Add the DBFile object to the collection oDatabase.FileGroups("PRIMARY").DBFiles.Add oDBFile
'Set the properties of the LogFile object oLogFile.Name = sDatabaseName & ".ldf" oLogFile.PhysicalName = sDBFilePath & ".ldf"
'Add the LogFile object to the collection oDatabase.TransactionLog.LogFiles.Add oLogFile
'Add the new DB object to the databases collection oSQLServer.Databases.Add oDatabase
CreateDB_Exit: On Error Resume Next
Set oLogFile = Nothing Set oDBFile = Nothing Set oDatabase = Nothing
MousePointer = vbDefault Exit SubCreateDB_Err: SQLDMOError Resume CreateDB_ExitEnd Sub
dim sql as string
set con=new connection
con.connectionstring="provider=msdasql;driver=sql server;server=" & ServerName & ";uid=" & Uid & ";pwd=" & Pwd & ";database=master;"
…………
con.open'创建数据库
sql="CREATE DATABASE DBname…………"
con.execute sql'创建表格
sql="use DBname Create Table TableName…………"
con.execute sql'创建视图
sql="use DBname Create View ViewName…………"
con.execute sql…………'连接到新建的数据库
Set con=new connection
con.connectionstring="provider=msdasql;driver=sql server;server=" & ServerName & ";uid=" & Uid & ";pwd=" & Pwd & ";database=DBname;"
…………
con.open
Private Sub cmdCreateDB_Click()
Dim oDatabase As New SQLDMO.Database
Dim oDBFile As New SQLDMO.DBFile
Dim oLogFile As New SQLDMO.LogFile
Dim sDatabaseName As String
Dim sDBFilePath As String
On Error GoTo CreateDB_Err
'Input the database name
sDatabaseName = InputBox("Enter the new database name", _
"New Database")
' Make sure that the database name isn't already used
On Error Resume Next
If oSQLServer.Databases(sDatabaseName).Name = "" Then
End If
If Err = 0 Then
MsgBox sDatabaseName & " already exists!"
GoTo CreateDB_Exit
Else
On Error GoTo CreateDB_Err
End If
'Set the object's database name
oDatabase.Name = sDatabaseName
'Input the path for the data and log files
sDBFilePath = InputBox("Enter the path for data and log files", _
"New Database", _
"c:\Program Files\Microsoft SQL Server\MSSQL\Data\" _
& sDatabaseName)
'Set the properties of the DBFile object
oDBFile.Name = sDatabaseName & ".mdf"
oDBFile.PhysicalName = sDBFilePath & ".mdf"
'Add the DBFile object to the collection
oDatabase.FileGroups("PRIMARY").DBFiles.Add oDBFile
'Set the properties of the LogFile object
oLogFile.Name = sDatabaseName & ".ldf"
oLogFile.PhysicalName = sDBFilePath & ".ldf"
'Add the LogFile object to the collection
oDatabase.TransactionLog.LogFiles.Add oLogFile
'Add the new DB object to the databases collection
oSQLServer.Databases.Add oDatabase
CreateDB_Exit:
On Error Resume Next
Set oLogFile = Nothing
Set oDBFile = Nothing
Set oDatabase = Nothing
MousePointer = vbDefault Exit SubCreateDB_Err:
SQLDMOError
Resume CreateDB_ExitEnd Sub
create databasename.dbo.tablename(col1 char(10),col2 char(20)...)
create table databasename.dbo.tablename(col1 char(10),col2 char(20)...)