SQL Server Assuming you have sufficient permissions to the SQL Server box, you can simply say:
<% set conn = CreateObject("ADODB.Connection") conn.open "<connection string to master>" sql = "CREATE DATABASE " & new_database_name conn.execute sql, , 129 ' create tables, etc... %> |
The owner of the database will be the user specified in UID. To change the database owner, use sp_changedbowner, as follows:
<% set conn = CreateObject("ADODB.Connection") conn.open "<connection string>" sql = "USE dummy; EXEC sp_changedbowner 'username'" conn.execute sql, , 129 ' create tables, etc... %> |
Now, if you have user-defined objects that you'd like automatically created every time you create a new database, put them in the model database. This database is used as a template for new databases.
Access Here is code that will create an empty Access database from ASP:
<% newDB = "c:\inetpub\wwwroot\databases\new.mdb" newDB = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & newDB Set cat = CreateObject("ADOX.Catalog") cat.Create newDB Set conn = CreateObject("ADODB.Connection") conn.Open newDB ' create tables, etc... %> |
Another way to do it (if you're creating cookie-cutter databases) is to generate a template with table structure and no data, and simply copy it:
<% targetDB = "c:\inetpub\wwwroot\databases\new.mdb" sourceDB = "c:\inetpub\wwwroot\databases\template.mdb" set fso = CreateObject("Scripting.FileSystemObject") fso.CopyFile sourceDB, targetDB, true set fso = nothing set conn = CreateObject("ADODB.Connection") conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & targetDB ' ... %> |
(Of course, you should check that the targetDB name is not already taken, or you could wipe out someone's data.)
Here is another set of samples that uses a bit more rigorous check for existing objects, and also creates a table in the new database:
This version allows you to create a database in Access. Note that there is no error checking.
<% db = Server.MapPath("/sampleDatabase.mdb") tableName = "sampleTable" 'if you have JET installed: dbCreate = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & db 'otherwise: 'dbCreate = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & db set cat = CreateObject("ADOX.Catalog") if not fso.fileExists(db) then cat.create dbCreate set conn = CreateObject("ADODB.Connection") conn.open dbCreate cat.activeConnection = conn tableExists = false for i=0 to cat.tables.count-1 typ = cat.tables(i).type nam = cat.tables(i).name If typ = "TABLE" and lcase(nam) = lcase(tName) then tableExists = true End if Next if not tableExists then tableCreate = "CREATE TABLE " & tName & "(" & _ "IDColumn AUTOINCREMENT," & _ "IntegerColumn INT," & _ "VarcharColumn VARCHAR(50)," & _ "MemoColumn MEMO DEFAULT '')" conn.execute tableCreate, , 129 end if conn.close set conn = nothing set cat = nothing %> |
And here is a version for SQL Server (note that you must connect to SQL Server with a user who has sufficient privileges to create a new database). While I don't generally advocate the use of dynamic SQL, the following procedure was concocted simply to demonstrate what you can do (and to avoid having to generate this SQL string within an ASP page!):
CREATE PROCEDURE dbo.userProc_buildDatabase @dbName SYSNAME, @tableName SYSNAME AS BEGIN IF NOT EXISTS ( SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE CATALOG_NAME=@dbName ) EXEC('CREATE DATABASE '+@dbName) EXEC('CREATE TABLE '+@dbName + '.dbo.'+@tableName +'(IDColumn INT IDENTITY(1,1), IntegerColumn INT, VarcharColumn VARCHAR(50), TextColumn TEXT DEFAULT '''')') END |
Now you can call it from ASP as follows:
<% db = "sampleDatabase" table = "sampleTable" set conn = CreateObject("ADODB.Connection") conn.open "<connection string>" sql = "EXEC Master.dbo.userProc_buildDatabase " & _ " @dbName='" & db & "'" & _ ",@tableName='" & table & "'" conn.execute sql, , 129 conn.close: set conn = nothing %> |