Create Database via Code behind(C#):-
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["db1"].ConnectionString);
}
protected void Database()
{
String txtdb = txtdatabase.Text;
SqlDataAdapter adpdb = new SqlDataAdapter("SELECT [name] FROM Master..sysdatabases where name ='"+txtdatabase.Text+"'",con);
DataSet dsdb = new DataSet();
adpdb.Fill(dsdb);
if (dsdb.Tables[0].Rows.Count>0)
{
Response.Write("Database Already Exits");
}
else
{
var conn = new SqlConnection("data source=Server Name; uid=username; pwd=password;");
String sql = "Create Database "+txtdatabase.Text+"; CREATE TABLE dbo.Table1 (ID int, Data, Data nvarchar(128));";
var cmd = new SqlCommand();
try
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "Create Database " + txtdatabase.Text + ";";
cmd.ExecuteNonQuery();
cmd.CommandText = "Use " + txtdatabase.Text + ";CREATE TABLE dbo.Table1 (ID int, Data nvarchar(128))";
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception expdb)
{
Response.Write(expdb.GetBaseException());
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["db1"].ConnectionString);
}
protected void Database()
{
String txtdb = txtdatabase.Text;
SqlDataAdapter adpdb = new SqlDataAdapter("SELECT [name] FROM Master..sysdatabases where name ='"+txtdatabase.Text+"'",con);
DataSet dsdb = new DataSet();
adpdb.Fill(dsdb);
if (dsdb.Tables[0].Rows.Count>0)
{
Response.Write("Database Already Exits");
}
else
{
var conn = new SqlConnection("data source=Server Name; uid=username; pwd=password;");
String sql = "Create Database "+txtdatabase.Text+"; CREATE TABLE dbo.Table1 (ID int, Data, Data nvarchar(128));";
var cmd = new SqlCommand();
try
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "Create Database " + txtdatabase.Text + ";";
cmd.ExecuteNonQuery();
cmd.CommandText = "Use " + txtdatabase.Text + ";CREATE TABLE dbo.Table1 (ID int, Data nvarchar(128))";
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception expdb)
{
Response.Write(expdb.GetBaseException());
}
}
}
No comments:
Post a Comment