aspx code
<body>
<form id="form1" runat="server">
<div>
<table style="width: 762px; height: 294px" border="1" bordercolor="#666600">
<tr>
<td colspan="3" style="height: 26px">
<strong><span style="font-size: 16pt">Web Based SQL Database Creation Wizard</span></strong></td>
</tr>
<tr>
<td colspan="3" valign="top">
<asp:Wizard ID="Wizard1" runat="server" BackColor="#F7F6F3" BorderColor="#CCCCCC"
BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em"
Height="145px" Width="738px" ActiveStepIndex="0">
<StepStyle BorderWidth="0px" ForeColor="#5D7B9D" />
<WizardSteps>
<asp:WizardStep ID="WizardStep1" runat="server" Title="Database Configuration">
<table style="width: 362px;" height="200">
<tr>
<td style="width: 396px; height: 211px" colspan="3">
<table style="width: 558px; height: 80px">
<tr>
<td style="width: 146px">
<asp:Label ID="Label1" runat="server" Text="Database Server"></asp:Label>
</td>
<td style="width: 74px">
<asp:TextBox ID="txtServer" runat="server"></asp:TextBox>
</td>
<td style="width: 2px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtServer" ErrorMessage="Database Server Required" Width="183px"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 146px">
<asp:Label ID="Label2" runat="server" Text="Database Name"></asp:Label>
</td>
<td style="width: 74px">
<asp:TextBox ID="txtDBName" runat="server">TestDB</asp:TextBox>
</td>
<td style="width: 2px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtDBName" ErrorMessage="Database Name Required" Width="192px"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 146px">
<asp:Label ID="Label5" runat="server" Text="Authentication Mode"></asp:Label>
</td>
<td style="width: 74px">
<asp:DropDownList ID="ddlAuthenticationMode" runat="server" AutoPostBack="True">
<asp:ListItem Selected="True" Value="0">SQL Server</asp:ListItem>
<asp:ListItem Value="1">Windows</asp:ListItem>
</asp:DropDownList>
</td>
<td style="width: 2px">
</td>
</tr>
<tr>
<td style="width: 146px">
<asp:Label ID="Label3" runat="server" Text="SQL User ID"></asp:Label>
</td>
<td style="width: 74px">
<asp:TextBox ID="txtUserID" runat="server"></asp:TextBox>
</td>
<td style="width: 2px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtUserID" ErrorMessage="Database User Name Required" Width="200px"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 146px">
<asp:Label ID="Label4" runat="server" Text="SQL Server Password"></asp:Label>
</td>
<td style="width: 74px">
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password">sa</asp:TextBox>
</td>
<td style="width: 2px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtPassword" ErrorMessage="SQL Password Required" Width="175px"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 146px">
</td>
<td style="width: 74px">
</td>
<td style="width: 2px">
</td>
</tr>
</table>
</td>
</tr>
</table>
</asp:WizardStep>
<asp:WizardStep ID="WizardStep2" runat="server" Title="Run SQL Script"><table style="width: 362px;" height="200">
<tr>
<td style="width: 358px; height: 211px" colspan="3" rowspan="5">
<table style="width: 336px">
<tr>
<td style="height: 18px">
<asp:Label ID="Label6" runat="server" Text="SQL Script Path:"></asp:Label>
</td>
<td style="height: 18px">
<asp:TextBox ID="txtSQLPath" runat="server" Width="176px" ReadOnly="True">Template/SQLFIle.sql</asp:TextBox>
</td>
<td style="width: 3px; height: 18px">
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td style="width: 3px">
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="Label7" runat="server" Text="Ex: Path Should be Server.Map Path Click on Finish to Run SQL Script File" Height="50px" Width="244px"></asp:Label>
</td>
<td style="width: 3px">
</td>
</tr>
</table>
</td>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
</table>
</asp:WizardStep>
</WizardSteps>
<SideBarButtonStyle BorderWidth="0px" Font-Names="Verdana" ForeColor="White" />
<NavigationButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"
BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284775" />
<SideBarStyle BackColor="#7C6F57" BorderWidth="0px" Font-Size="0.9em" VerticalAlign="Top" />
<HeaderStyle BackColor="#5D7B9D" BorderStyle="Solid" Font-Bold="True" Font-Size="0.9em"
ForeColor="White" HorizontalAlign="Left" />
</asp:Wizard>
</td>
</tr>
<tr>
<td colspan="3">
</td>
</tr>
</table>
</div>
</form>
C# code
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using System.Collections;
using System.Data;
using System.Diagnostics;
using Microsoft.SqlServer;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Common.ConnectionManager;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
DatabaseCreator objDBCreate = default(DatabaseCreator);
string ConnStr = "";
bool IsDBCreated = false;
protected void Wizard1_FinishButtonClick(object sender, System.Web.UI.WebControls.WizardNavigationEventArgs e)
{
if (Wizard1.ActiveStep.Name == "Run SQL Script")
{
string sqlpath = Server.MapPath(txtSQLPath.Text);
objDBCreate = new DatabaseCreator();
//======== First Create the Connection String from Previous Steps Settings
string StrCon = "";
if ((ViewState("ConStr") != null))
{
StrCon = ViewState("ConStr") + "Database=" + txtDBName.Text + ";";
}
//========= Run the SQL Script
if (objDBCreate.ExecuteSQLScript(sqlpath, StrCon))
{
CreateMessageAlert(this.Page, "SQL Script Execute Successfully..", "Run");
}
else
{
CreateMessageAlert(this.Page, "There is an error into script file", "notrun");
}
}
}
protected void Wizard1_NextButtonClick(object sender, System.Web.UI.WebControls.WizardNavigationEventArgs e)
{
//======== Create Database
if (Wizard1.ActiveStep.Name == "Database Configuration")
{
//========= Check DB Exits or not
if (CheckAndCreateDatabase(txtServer.Text, txtUserID.Text, txtPassword.Text, txtDBName.Text) == true)
{
IsDBCreated = true;
CreateMessageAlert(this.Page, "Database Created Successfully.", "Created");
}
else
{
}
}
}
private bool CheckAndCreateDatabase(string servername, string userid, string password, string database)
{
if (TestConnection(servername, userid, password, database) == true)
{
//check Server name and database name are not empty.
if (!string.IsNullOrEmpty(servername) & !string.IsNullOrEmpty(database))
{
Server oServer = new Server();
Management.Common.ServerConnection oServerConnection = new Management.Common.ServerConnection(servername);
oServerConnection.ConnectionString = ConnStr;
oServerConnection.Connect();
oServer = new Server(oServerConnection);
Database db = new Database();
//========= check for DB Existance
foreach ( db in oServer.Databases)
{
if (database == db.Name)
{
CreateMessageAlert(this.Page, "The database already exist, try with another name..", "Database");
return false;
}
}
//create the database procedure goes here.
objDBCreate = new DatabaseCreator();
if (objDBCreate.CreateSQLDb(txtServer.Text, txtDBName.Text, txtUserID.Text, txtPassword.Text) == true)
{
ViewState.Add("ConStr", ConnStr);
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
private bool TestConnection(string servername, string userid, string password, string databasename)
{
//check for windows authentication mode.
if (ddlAuthenticationMode.SelectedValue == 1)
{
ConnStr = "Persist Security Info=False;Integrated Security=SSPI;Data Source=" + servername + ";";
}
//check for sql server authentication mode.
if (ddlAuthenticationMode.SelectedValue == 0)
{
ConnStr = "Persist Security Info=False;User ID=" + userid + ";pwd=" + password + ";Data Source=" + servername + ";";
}
string str = ConnStr;
Data.SqlClient.SqlConnection Conn = new Data.SqlClient.SqlConnection();
Conn.ConnectionString = ConnStr;
if (Conn.State == Data.ConnectionState.Closed)
{
Conn.Open();
return true;
} else {
CreateMessageAlert(this.Page, "Test connection failed for Server : " + txtServer.Text, "Database");
return false;
}
}
protected void ddlAuthenticationMode_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (ddlAuthenticationMode.SelectedValue == 0) {
RequiredFieldValidator3.Enabled = true;
RequiredFieldValidator4.Enabled = true;
txtUserID.Enabled = true;
txtPassword.Enabled = true;
} else {
RequiredFieldValidator3.Enabled = false;
RequiredFieldValidator4.Enabled = false;
txtUserID.Enabled = false;
txtPassword.Enabled = false;
}
}
}
error
A using namespace directive can only be applied to namespaces; 'Microsoft.SqlServer.Management.Common.ConnectionManager' is a type not a namespace
Source Error:
Line 12: using Microsoft.SqlServer.Management.Smo;
Line 13: using Microsoft.SqlServer.Management.Common;
Line 14: using Microsoft.SqlServer.Management.Common.ConnectionManager;
Line 15:
Line 16: public partial class _Default : System.Web.UI.Page