Click here to Skip to main content
12,623,511 members (27,867 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#3.0 C#
Hi,
I have one Web form clicking on save button i want to create DB and Some Sql Script i want to execute under that DB which ever i created.
I coded like this but it is throwing error .
Error is Failure to connect Server name ,DB name ,USer ID and Password it is throwing.
protected void DBCreation()
        {
           
 
            System.Data.SqlClient.SqlConnection tmpConn;
            string sqlCreateDBQuery;
            tmpConn = new SqlConnection();
            DataSet ds = new DataSet();
            ds = clsNewcompany.GetServerName();
            //string conn = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;               
            string ServerName = ds.Tables[0].Rows[0]["ServerName"].ToString();
            //SqlConnection sqlConn = new SqlConnection("server= " + ServerName + " ;uid=sa;pwd=myPassword;database=master");
            tmpConn.ConnectionString = "SERVER = " + ServerName +"; DATABASE = master; User ID = sa; Pwd = chirag12";
            sqlCreateDBQuery = " CREATE DATABASE "
                               + txtCompany.Text.Trim().ToString();
            FileInfo file = new FileInfo(@"E:\tab.sql");
            SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery, tmpConn);
            try
            {
                tmpConn.Open();
                myCommand.ExecuteNonQuery();
                tmpConn.Close();
                string script = file.OpenText().ReadToEnd();
                tmpConn.ConnectionString = "SERVER = " + ServerName + "; DATABASE = " + txtCompany.Text.Trim().ToString() + "; User ID = sa; Pwd = chirag12";           
                //tmpConn.ConnectionString = "Server = " + ServerName + "; Database = HP; User ID = sa; Pwd = chirag12";
                Server server = new Server(new ServerConnection(tmpConn.ConnectionString));
                tmpConn.Open();
                server.ConnectionContext.ExecuteNonQuery(script);
                file.OpenText().Close();
                //tmpConn.Close();

            }
            catch (Exception ex)
            {
 
            }
        }
Please help me any one....
Posted 20-Nov-12 5:13am
Updated 20-Nov-12 6:10am
RyanDev229.3K
v2
Comments
ryanb31 20-Nov-12 11:11am
   
I didn't read through all of the code but are you trying to connect to the db that doesn't exist yet? You'll need to connect to the masterdb, create the db, then you can change the connection to point to the new db.
BalaMahesh 21-Nov-12 2:00am
   
I tried in some another way its working fine thanks,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161128.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100