Link to home
Start Free TrialLog in
Avatar of SwingSt
SwingSt

asked on

Create Database Programatically in asp.net

I need to be able to code a page for an asp.net website (using vb.net) that can a:) Create a database based on user input (name of db only) and b:) create it in my sites "App_Data" folder. I've been able to create a database using the code below, but it places it in the default SQL Server folder, rather than the App_Data folder. I am using VS 2008 and SQL Server 2005. So how can I arrange for the created database to be in my sites "App_Data" folder and attach it to the project.
dim c as sqlconnection
dim com as sqlcommand
dim w as string = "Create Database " & "textbox1.text
 
com = new sqlcommand(w,c)
c.open
com.executenonquery
c.close

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Solar_Flare
Solar_Flare

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SwingSt
SwingSt

ASKER

No, these db's are to be original. The link to MSDN would be great, but when I use any of the examples other than
     dim w as string = "create database " & fileName.text
I start getting errors like the following:

Incorrect syntax near 'FILENAME'

So, could I get an example of the proper syntax one would use in this situation?
Avatar of SwingSt

ASKER

Nevermind, I finally worked out the syntax, thanks for the help - pushed me in the right direction. For anyone looking for a similar solution - I have posted the final code below. It creates a brand new Db in the folder specified, you have to then create and your needed tables but it is right there.
dim c as sqlconnection = YOUR CONNECTION
dim com as sqlcommand
Dim w As String
        w = "CREATE DATABASE " & nameBox.Text & " " _
        & "ON " _
        & "( NAME = " & nameBox.Text & "," _
        & " FILENAME = 'C:\Users\Thomas\Desktop\WebSite1\App_Data\" & nameBox.Text & ".mdf')" _
        & " LOG ON" _
        & " ( NAME = '" & nameBox.Text & "_log'," _
        & " FILENAME = 'C:\Users\Thomas\Desktop\WebSite1\App_Data\" & nameBox.Text & ".ldf')"
 
com = new sqlcommand(w,c)
c.open
com.executenonquery
c.close

Open in new window