protected void btnCreate_Click(object sender, EventArgs e)
    {

        try
        {
            server = txtServer.Text.ToString();
            database = txtData.Text.ToString();
            userid = txtUser.Text.ToString();
            password = txtPassword.Text.ToString();
            try
            {
                System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

                System.Configuration.KeyValueConfigurationElement setting = config.AppSettings.Settings["MyValue"];

                if (null != setting)
                {
                    //config.AppSettings.Settings["MyValue"].Value = txtServer.Text;
                    config.AppSettings.Settings["connString"].Value =( "Data Source=" + server + ";Initial Catalog=" + database + "; User ID="+ userid + "; Password =" + password +";");

                }
                else
                {
                    config.AppSettings.Settings.Add("connString", ("Data Source=" + server + ";Initial Catalog=" + database + "; User ID=" + userid + "; Password =" + password + ";"));
                }

                config.Save();
                lblMsg.Text = "New value saved. Application will be restarted automatically.";
            }
            catch (System.Exception exc)
            {
                lblMsg.Text = (exc.Message + "<br />" + exc.StackTrace).Replace("\n", "<br />");
            }


 string path = Server.MapPath("~/sqlquery/");
            string[] filePaths = Directory.GetFiles(path);
            SqlConnection con = new SqlConnection();
            SqlCommand cmd1 = new SqlCommand("CREATE DATABASE " + database, con);
            //con.Open();
            cmd1.ExecuteNonQuery();
            foreach (string filepath in filePaths)
            {
                StreamReader sr = new StreamReader(filepath);
                string st = sr.ReadToEnd();
                SqlCommand cmd = new SqlCommand(st, con);
                cmd.ExecuteNonQuery();
            }

           // con.Close();

        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

Comments

Popular posts from this blog

ASP.NET Interview Questions – Beginner Level (Part 1)

Asp Login Form