Click here to Skip to main content
Licence 
First Posted 27 Oct 2007
Views 36,684
Downloads 633
Bookmarked 29 times

Create SQL Server Database using asp.net

By | 27 Oct 2007 | Article
Here I am showing, How to Create SQL Database and Run SQL Script File Using asp.net.

Fill SQL Server Authentication Information

Screenshot - Server_Settings.jpg

SQL Script File Path

Screenshot - SQL_Script_Path.jpg

Introduction

To create SQL Database using asp.net is quite difficult, Here I am writing
how to create SQL Database and how to Run the SQL Script File using asp.net.
To Run this Database Creation wizard, you have to Enter the Required input
and finally you will get Database created.

Here you can see two Screenshot. The first one is showing the SQL Server
Configuration that includes the ( SQL Server Name, Database Name,
SQL User ID and SQL Server Password).

And Second screen is showing the Virtual Path of SQL Script file.

Background

Web Based SQL Database Creation Wizard is looking Odd Requirements.
But Currently I am developing one Web Application, and my Requirement
was to Create New SQL Database and run the SQL Script File that
includes Tables, View and Stored Procedures. After Database Creation,
I have to work with that Database. So I found a lot of internet,
but I didn't find any good article or resources for this, so finally
I have created this Wizard application for those who had the same
requirements like this.

Using the code

First of all you required following namespace.

Imports Microsoft.SqlServer
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common.ConnectionManager

And with the help of Classes, I have Created one DatabaseCreator.vb Class,
that contains the methods to Create SQL Database and Execute SQL Script Files.

To Create SQL Database, you have to Create the object of this class and
call required methods.


EX:
Dim objDBCreate as New DatabaseCreator
And call CreateSQLDb() Function with required Paramters, This function will
return true , if database will successfully created, otherwise it will return False.

objDBCreate.CreateSQLDb("SqlServerName", "DBName", "SQLUserID", "SQLPWD")

now after Create Database, on Next Step, we Execute the SQL Script File
by calling this Method: ExecuteSQLScript()

it takes two parameters, One is SQL Script File Path, and the Path
Should be Physical path, so you can get path using
Dim sqlpath As String = Server.MapPath("Virtual Path of SQL Script")And Second one is Connection String of Newly Created database.

Here check out little bit code. To Get full souce of Code, please download from Top link.

//
//This Code will create SQL Db 
//create the database procedure goes here.

objDBCreate = New DatabaseCreator
If objDBCreate.CreateSQLDb(txtServer.Text, txtDBName.Text, txtUserID.Text, _
    txtPassword.Text) = True Then
    ViewState.Add("ConStr", ConnStr)
    Return True
Else
    Return False
End If

//
// This Code will Run the SQL Script File
//

Dim sqlpath As String = Server.MapPath(txtSQLPath.Text)
objDBCreate = New DatabaseCreator
If objDBCreate.ExecuteSQLScript(sqlpath, StrCon) Then
  CreateMessageAlert(Me.Page, "SQL Script Execute Successfully..", "Run")
Else
  CreateMessageAlert(Me.Page, "There is an error into script file", "notrun")
End If

// 

Now, hope you get code to create SQLDB using DatabaseCreator.vb Class.

Conclusion

So this way you can Create SQL database using asp.net, Please feel free to post your comments or messages for any query or further information. :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jatin Prajapati

Team Leader

India India

Member

Jatin is Working in .Net Technology Since 2006. He has Completed Master of Science Degree in Information Technology. He Likes to learn Cutting edge technologies. He had good Skills in Asp.net, Vb.net,C#.Net, Crystal Reports,GDI+, Atlas.Net, SQL Server,IIS Admin,VSS Admin, Application Architecture Designing.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHi Jatin, [modified] Pinmemberjain_nitin1:56 7 Oct '09  
GeneralConnection string PinmemberBijayBhasker19:41 20 Sep '09  
Questionwould appreciate help Pinmemberts1112084:52 14 Nov '08  
GeneralNice code Pinmembersrinivassnv23:24 25 Sep '08  
GeneralRe: Nice code PinmemberJatin Prajapati2:31 26 Sep '08  
GeneralOH! realy perfect code Pinmemberprokurs3:55 22 Sep '08  
GeneralRe: OH! realy perfect code PinmemberJatin Prajapati3:34 23 Sep '08  
QuestionQuestion on Web based My Sql DB Creation wizard Pinmemberemilng8420:06 21 Sep '08  
AnswerRe: Question on Web based My Sql DB Creation wizard PinmemberJatin Prajapati3:35 23 Sep '08  
GeneralExcellent app PinmemberRazi Syed12:16 1 Nov '07  
GeneralRe: Excellent app PinmemberJatin Prajapati2:37 2 Nov '07  
GeneralInteresting Quick Read PinmemberPaul Conrad8:58 27 Oct '07  
GeneralRe: Interesting Quick Read PinmemberJatin Prajapati17:33 27 Oct '07  
GeneralRe: Interesting Quick Read PinmemberPaul Conrad19:03 27 Oct '07  
Questionwhat if I dont have SMO Pinmembermascix6:35 27 Oct '07  
AnswerRe: what if I dont have SMO PinmemberJatin Prajapati6:40 27 Oct '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 27 Oct 2007
Article Copyright 2007 by Jatin Prajapati
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid