Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
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 4:13am
Edited 20-Nov-12 5:10am
114.8K
v2
Comments
ryanb31 at 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 at 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
0 Sergey Alexandrovich Kryukov 487
1 CPallini 345
2 OriginalGriff 240
3 George Jonsson 159
4 Abdul Samad KP 145
0 OriginalGriff 6,329
1 Sergey Alexandrovich Kryukov 5,700
2 CPallini 4,940
3 George Jonsson 3,469
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2014
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