Introduction
Make your ASP.NET application install it's own Database<o:p>
<o:p>
<o:p>Clieck here to Download sourcecode
<o:p>
This article explains you that how we have to configure that application installs its own database.<o:p>
In this article I will explains you that by taking an application called “SetUpDataBase”<o:p>
In this application I have been created form called “DatabaseSetUp.aspx” in this form I am asking user to enter all SQL Server credentials like SQL SERVER Name, Database name and SQL Server authentication it includes username and password.<o:p>
<o:p>
As soon as User Enter the All SQL SERVER credentials after clicking the INSTALL button It executes the method called CreateDatabase();<o:p>
In This Method first I will check for Database what user enters already exists or not.<o:p>
If database already exists I will display the Error message other wise will create new database in SQL Server.<o:p>
<o:p>
string strSQL ="IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = '" + txtSqlDataBase.Text + "') SELECT 1 as EXTANT ELSE SELECT 0 as EXTANT";<o:p>
SqlCommand sqlCmd1 = new SqlCommand(strSQL,sqlConn);<o:p>
sqlCmd1.CommandType = CommandType.Text;<o:p>
//sqlConn.Open();<o:p>
<o:p>
int i = (int)sqlCmd1.ExecuteScalar();<o:p>
<o:p>
if(i==1)<o:p>
{<o:p>
<o:p>
rorMsg.Text ="DataBase Not Configured Sucessfully Contact DataBase Administrator";<o:p>
return;<o:p>
}<o:p>
else<o:p>
{<o:p>
ErrorMsg.Text="";<o:p>
strSQL = "CREATE DATABASE " + txtSqlDataBase.Text; <o:p>
SqlCommand sqlCmd = new SqlCommand(strSQL,sqlConn); <o:p>
try<o:p>
{<o:p>
sqlCmd.ExecuteNonQuery();<o:p>
}<o:p>
catch<o:p>
{<o:p>
<o:p>
ErrorMsg.Text ="DataBase Not Configured Sucessfully Contact DataBase Administrator";<o:p>
return;<o:p>
<o:p>
}<o:p>
<o:p>
After creating the Database I will call one more method called CreateStructureNData().<o:p>
In this method I will read the script files in side the _SQLDB Folder by using StreamReader.<o:p>
StreamReader sr1 = new StreamReader(RootDirectory +this.cSqlDataFile);<o:p>
string strSQL1 = sr1.ReadToEnd();<o:p>
SqlCommand cmd1 = new SqlCommand(strSQL1,sql1);<o:p>
cmd1.CommandType = CommandType.Text;<o:p>
try<o:p>
{<o:p>
cmd1.ExecuteNonQuery();<o:p>
}<o:p>
catch(Exception ex)<o:p>
{<o:p>
ErrorMsg.Text =ex.Message;<o:p>
return false;<o:p>
}<o:p>
sr1.Close();<o:p>
<o:p>
After creating Database and its Objects I will updates the web.config file by using the method called Configfilesetup ().<o:p>
In this method I update the SQL connection string as Follows,<o:p>
public void Configfilesetup()<o:p>
{<o:p>
try<o:p>
{<o:p>
XmlConfig.Load(Server.MapPath("~/Web.config")); <o:p>
XmlNode node = XmlConfig.SelectSingleNode("//appSettings");<o:p>
node.ChildNodes.Item(1).Attributes.Item(1).Value = txtSqlServer.Text;<o:p>
node.ChildNodes.Item(2).Attributes.Item(1).Value = txtSqlPort.Text;<o:p>
node.ChildNodes.Item(3).Attributes.Item(1).Value = txtSqlDataBase.Text;<o:p>
node.ChildNodes.Item(4).Attributes.Item(1).Value = txtSqlUserName.Text;<o:p>
node.ChildNodes.Item(5).Attributes.Item(1).Value = TxtPassword.Text;<o:p>
///////////<o:p>
//System.IO.FileInfo<o:p>
System.IO.FileInfo sFileInfo = new System.IO.FileInfo(Server.MapPath("~/Web.config")); <o:p>
sFileInfo.Attributes = FileAttributes.Normal; <o:p>
XmlConfig.Save(Server.MapPath("~/Web.config"));<o:p>
sFileInfo.Attributes = FileAttributes.ReadOnly;<o:p>
//////////<o:p>
<o:p>
<o:p>
}<o:p>
catch(Exception ex)<o:p>
{<o:p>
ErrorMsg.Text = ex.Message;<o:p>
}<o:p>
<o:p>
}<o:p>
In Page_Load () if already web.config files holds database connection settings I will fill all data into the form by using following code.<o:p>
if(!IsPostBack)<o:p>
{<o:p>
// Put user code to initialize the page here<o:p>
XmlConfig.Load(Server.MapPath("~/Web.config")); <o:p>
XmlNode node = XmlConfig.SelectSingleNode("//appSettings");<o:p>
txtSqlServer.Text = node.ChildNodes.Item(1).Attributes.Item(1).Value ;<o:p>
txtSqlPort.Text = node.ChildNodes.Item(2).Attributes.Item(1).Value;<o:p>
txtSqlDataBase.Text = node.ChildNodes.Item(3).Attributes.Item(1).Value ;<o:p>
txtSqlUserName.Text = node.ChildNodes.Item(4).Attributes.Item(1).Value;<o:p>
TxtPassword.Text = node.ChildNodes.Item(5).Attributes.Item(1).Value;<o:p>
}<o:p>
<o:p>
In web.config file we need to capture SQL Server connection credentials as follows,<o:p>
<appSettings><o:p>
<!—Connection string--><o:p>
<add key="SqlServer" value=" " /><o:p>
<add key="SqlServerPort" value="" /><o:p>
<add key="SqlDatabase" value=" " /><o:p>
<add key="SqlUserName" value=" " /><o:p>
<add key="SqlUserPassword" value=" " /><o:p>
</appSettings><o:p>
<o:p>
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.