Пример #1
0
    protected void btn_create_database_Click(object sender, EventArgs e)
    {
        if (txt_database_name.Text.Trim() == "")
        {
            lb_create_database_msg.ForeColor = System.Drawing.Color.Red;
            lb_create_database_msg.Text      = "Enter a database name.";
        }
        else if (ddl_select_website.SelectedItem.Text.Equals("Select"))
        {
            lb_create_database_msg.ForeColor = System.Drawing.Color.Red;
            lb_create_database_msg.Text      = "Select a website.";
        }
        else
        {
            lb_create_database_msg.Text = "";
            System.Web.Security.MembershipUser mu;
            Guid userid;
            mu     = System.Web.Security.Membership.GetUser();
            userid = (Guid)mu.ProviderUserKey;
            string username = Session[Constants.Session.USERNAME].ToString();

            string dbname      = txt_database_name.Text.Trim();
            string websitename = ddl_select_website.SelectedItem.Text.ToString();

            int websiteid = GetWebsiteId(userid, username, websitename);

            if (Session["databaseid"] != null)
            {
                if (dbmanagement.CheckDatabaseByDbNameAndWebsite(dbname, websitename))
                {
                    Clear();
                    btn_create_database.Text         = "Create Database";
                    lb_create_database_msg.ForeColor = System.Drawing.Color.Green;
                    lb_create_database_msg.Text      = "Database updated successfully.";
                    Session["databaseid"]            = null;
                    hf_db_id.Value = "";
                }
                else
                {
                    int dbid = dbmanagement.GetDbIdByWebsiteName(websitename);
                    if (dbid == Convert.ToInt32(hf_db_id.Value))
                    {
                        if (dbmanagement.UpdateDbByDBId(Convert.ToInt32(hf_db_id.Value), dbname, websiteid, userid, websitename, username))
                        {
                            Clear();
                            btn_create_database.Text         = "Create Database";
                            lb_create_database_msg.ForeColor = System.Drawing.Color.Green;
                            lb_create_database_msg.Text      = "Database updated successfully.";
                            Session["databaseid"]            = null;
                            hf_db_id.Value = "";
                        }
                    }
                    else
                    {
                        if (dbmanagement.IsDbCreatedForWebsite(websitename))
                        {
                            Clear();
                            lb_create_database_msg.ForeColor = System.Drawing.Color.Red;
                            lb_create_database_msg.Text      = "Could not attach database with this website because you have already created a database for this website.";
                            Session["databaseid"]            = null;
                            hf_db_id.Value = "";
                        }
                        else
                        {
                            if (dbmanagement.UpdateDbByDBId(Convert.ToInt32(hf_db_id.Value), dbname, websiteid, userid, websitename, username))
                            {
                                Clear();
                                btn_create_database.Text         = "Create Database";
                                lb_create_database_msg.ForeColor = System.Drawing.Color.Green;
                                lb_create_database_msg.Text      = "Database updated successfully.";
                                Session["databaseid"]            = null;
                                hf_db_id.Value = "";
                            }
                            else
                            {
                                Clear();
                                lb_create_database_msg.ForeColor = System.Drawing.Color.Red;
                                lb_create_database_msg.Text      = "Could not update. Please try again.";
                                Session["databaseid"]            = null;
                                hf_db_id.Value = "";
                            }
                        }
                    }
                }
            }
            else
            {
                //checking for is database created for this website or not?
                if (dbmanagement.IsDbCreatedForWebsite(websitename))
                {
                    lb_create_database_msg.ForeColor = System.Drawing.Color.Red;
                    lb_create_database_msg.Text      = "You have already created a database for this website.";
                }
                else
                {
                    if (dbmanagement.CheckDatabaseByDbNameAndWebsite(dbname, websitename))        // checking for updates.  if record exist already then update the database
                    {
                        //   update the database with new change..........
                        if (dbmanagement.UpdateDbRecord(dbname, websitename))
                        {
                            Clear();
                            btn_create_database.Text         = "Create Database";
                            lb_create_database_msg.ForeColor = System.Drawing.Color.Green;
                            lb_create_database_msg.Text      = "Database updated successfully.";
                        }
                        else
                        {
                            lb_create_database_msg.ForeColor = System.Drawing.Color.Red;
                            lb_create_database_msg.Text      = "Could not update database. Please try again.";
                        }
                    }
                    else
                    {
                        //if (dbmanagement.Save(dbname, websiteid, websitename, userid, username))
                        {
                            //    Clear();
                            //    lb_create_database_msg.ForeColor = System.Drawing.Color.Green;
                            //    lb_create_database_msg.Text = "Database created successfully.";

                            // code to create database.........................-----------------------
                            String        str;
                            SqlConnection myConn  = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True");
                            string        db_path = "~/web-design/" + Session[Constants.Session.USERNAME].ToString() + "/" + websitename + "/" + "App_Data/";
                            bool          exists  = System.IO.Directory.Exists(Server.MapPath(db_path));
                            Random        r       = new Random();
                            var           random  = r.Next(9999);
                            if (!exists)
                            {
                                System.IO.Directory.CreateDirectory(Server.MapPath(db_path));
                            }
                            str = "CREATE DATABASE " + dbname + websiteid + " ON PRIMARY " +
                                  "(NAME = " + dbname + websitename + ", " +
                                  "FILENAME = '" + Server.MapPath(db_path + dbname + ".mdf") + "', " +
                                  "SIZE = 4MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
                                  "LOG ON (NAME = " + dbname + ", " +
                                  "FILENAME = '" + Server.MapPath(db_path + dbname + "_log.ldf") + "', " +
                                  "SIZE = 1MB, " +
                                  "MAXSIZE = 5MB, " +
                                  "FILEGROWTH = 10%)";

                            SqlCommand myCommand = new SqlCommand(str, myConn);
                            try
                            {
                                myConn.Open();
                                myCommand.ExecuteNonQuery();
                                // entering details in db-----------------
                                if (dbmanagement.Save(dbname, websiteid, websitename, userid, username))
                                {
                                    Clear();
                                    lb_create_database_msg.ForeColor = System.Drawing.Color.Green;
                                    lb_create_database_msg.Text      = "Database created successfully.";
                                }
                                //---------------------
                                //  MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            catch (System.Exception ex)
                            {
                                lb_create_database_msg.ForeColor = System.Drawing.Color.Red;
                                lb_create_database_msg.Text      = "Choose database name atleast 10 characters long.";
                                // MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            finally
                            {
                                if (myConn.State == ConnectionState.Open)
                                {
                                    myConn.Close();
                                }
                            }
                            //----------------------------------
                        }
                        //else
                        //{
                        //    lb_create_database_msg.ForeColor = System.Drawing.Color.Red;
                        //    lb_create_database_msg.Text = "Could not created. Please try again.";
                        //}
                    }
                }
            }
        }
    }