Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. This May Be A Stupid Question But... [modified] Found It!

This May Be A Stupid Question But... [modified] Found It!

Scheduled Pinned Locked Moved Database
databasetutorialquestionsql-serversysadmin
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Roger Wright
    wrote on last edited by
    #1

    I'll ask it anyway, having already tried Google and MSDN. I'm fiddling with a SQL Server app to track substation equipment - one of several projects I'm working on. I've got a skeleton working on a local instance of SQL Server Express, but I'm thinking ahead to the day I deploy this on the company server, which has the full version of SQL Server installed. I don't want to manually recreate the database on the server - that way madness lies - and I don't want to Export/Import my existing database, as it's full of test cases I don't want to have to delete. I'm sure there's a nice, efficient way to create a database in SQL Server programmatically when my app runs for the first time, but I haven't a clue how to do it. Google returns a plethora of responses, enough to completely confuse me, and MSDN returns none of any relevance - the usual ratio. Can someone point me to a tutorial of some sort that my aged brain can comprehend? [EDIT] While MSDN didn't help, I finally asked the right question in Google - which led straight back to Microsoft, of course:

    using System;
    using System.Data.SqlClient;
    String str;
    SqlConnection myConn = new SqlConnection ("Server=localhost;Integrated security=SSPI;database=master");

    str = "CREATE DATABASE MyDatabase ON PRIMARY " +
        "(NAME = MyDatabase\_Data, " +
        "FILENAME = 'C:\\\\MyDatabaseData.mdf', " +
        "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
        "LOG ON (NAME = MyDatabase\_Log, " +
        "FILENAME = 'C:\\\\MyDatabaseLog.ldf', " +
        "SIZE = 1MB, " +
        "MAXSIZE = 5MB, " +
        "FILEGROWTH = 10%)";
    
    SqlCommand myCommand = new SqlCommand(str, myConn);
    try 
    {
        myConn.Open();
    myCommand.ExecuteNonQuery();
    MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK,    MessageBoxIcon.Information);
    }
    catch (System.Exception ex)
    {
    MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    finally
    {
    if (myConn.State == ConnectionState.Open)
    {
        myConn.Close();
    }
    }
    

    Sorry about the formatting - that's Opera at work... The article further states that, if I want just to use the Model database, eliminate all the crud and use simply, str = "CREATE DATABASE MyDatabase". I haven't tried it yet as it's getting rather late, but I think I'll play a bit later this week. I presume that I can then use CREATE TABLE and all its sundry relative

    M P J P C 5 Replies Last reply
    0
    • R Roger Wright

      I'll ask it anyway, having already tried Google and MSDN. I'm fiddling with a SQL Server app to track substation equipment - one of several projects I'm working on. I've got a skeleton working on a local instance of SQL Server Express, but I'm thinking ahead to the day I deploy this on the company server, which has the full version of SQL Server installed. I don't want to manually recreate the database on the server - that way madness lies - and I don't want to Export/Import my existing database, as it's full of test cases I don't want to have to delete. I'm sure there's a nice, efficient way to create a database in SQL Server programmatically when my app runs for the first time, but I haven't a clue how to do it. Google returns a plethora of responses, enough to completely confuse me, and MSDN returns none of any relevance - the usual ratio. Can someone point me to a tutorial of some sort that my aged brain can comprehend? [EDIT] While MSDN didn't help, I finally asked the right question in Google - which led straight back to Microsoft, of course:

      using System;
      using System.Data.SqlClient;
      String str;
      SqlConnection myConn = new SqlConnection ("Server=localhost;Integrated security=SSPI;database=master");

      str = "CREATE DATABASE MyDatabase ON PRIMARY " +
          "(NAME = MyDatabase\_Data, " +
          "FILENAME = 'C:\\\\MyDatabaseData.mdf', " +
          "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
          "LOG ON (NAME = MyDatabase\_Log, " +
          "FILENAME = 'C:\\\\MyDatabaseLog.ldf', " +
          "SIZE = 1MB, " +
          "MAXSIZE = 5MB, " +
          "FILEGROWTH = 10%)";
      
      SqlCommand myCommand = new SqlCommand(str, myConn);
      try 
      {
          myConn.Open();
      myCommand.ExecuteNonQuery();
      MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK,    MessageBoxIcon.Information);
      }
      catch (System.Exception ex)
      {
      MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
      }
      finally
      {
      if (myConn.State == ConnectionState.Open)
      {
          myConn.Close();
      }
      }
      

      Sorry about the formatting - that's Opera at work... The article further states that, if I want just to use the Model database, eliminate all the crud and use simply, str = "CREATE DATABASE MyDatabase". I haven't tried it yet as it's getting rather late, but I think I'll play a bit later this week. I presume that I can then use CREATE TABLE and all its sundry relative

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Roger Wright wrote:

      I'm sure there's a nice, efficient way to create a database in SQL Server

      Not programatically but there are 2 very simple ways to move a copy of your database to the production server. You can either backup your express version and restore it to the server or take a copy of the .mdf and .ldf files to the server and attach them. I think the attach one has the benefit of bringing the diagram which the backup/restore misses. Both are VERY simple processes.

      Never underestimate the power of human stupidity RAH

      R 1 Reply Last reply
      0
      • R Roger Wright

        I'll ask it anyway, having already tried Google and MSDN. I'm fiddling with a SQL Server app to track substation equipment - one of several projects I'm working on. I've got a skeleton working on a local instance of SQL Server Express, but I'm thinking ahead to the day I deploy this on the company server, which has the full version of SQL Server installed. I don't want to manually recreate the database on the server - that way madness lies - and I don't want to Export/Import my existing database, as it's full of test cases I don't want to have to delete. I'm sure there's a nice, efficient way to create a database in SQL Server programmatically when my app runs for the first time, but I haven't a clue how to do it. Google returns a plethora of responses, enough to completely confuse me, and MSDN returns none of any relevance - the usual ratio. Can someone point me to a tutorial of some sort that my aged brain can comprehend? [EDIT] While MSDN didn't help, I finally asked the right question in Google - which led straight back to Microsoft, of course:

        using System;
        using System.Data.SqlClient;
        String str;
        SqlConnection myConn = new SqlConnection ("Server=localhost;Integrated security=SSPI;database=master");

        str = "CREATE DATABASE MyDatabase ON PRIMARY " +
            "(NAME = MyDatabase\_Data, " +
            "FILENAME = 'C:\\\\MyDatabaseData.mdf', " +
            "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
            "LOG ON (NAME = MyDatabase\_Log, " +
            "FILENAME = 'C:\\\\MyDatabaseLog.ldf', " +
            "SIZE = 1MB, " +
            "MAXSIZE = 5MB, " +
            "FILEGROWTH = 10%)";
        
        SqlCommand myCommand = new SqlCommand(str, myConn);
        try 
        {
            myConn.Open();
        myCommand.ExecuteNonQuery();
        MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK,    MessageBoxIcon.Information);
        }
        catch (System.Exception ex)
        {
        MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        finally
        {
        if (myConn.State == ConnectionState.Open)
        {
            myConn.Close();
        }
        }
        

        Sorry about the formatting - that's Opera at work... The article further states that, if I want just to use the Model database, eliminate all the crud and use simply, str = "CREATE DATABASE MyDatabase". I haven't tried it yet as it's getting rather late, but I think I'll play a bit later this week. I presume that I can then use CREATE TABLE and all its sundry relative

        P Offline
        P Offline
        Peter_in_2780
        wrote on last edited by
        #3

        I haven't done it in your kind of environment, but the following approach has worked for me elsewhere:

        connect_to_server();
        if (error_db_not_exist == open_database(my_db))
        {
        read_from_file(canned_sql);
        run_query(canned_sql); // maybe multiple queries to run
        if (success != open_database(my_db))
        // oh, @#$%^&*!
        }

        where canned_sql is the appropriate set of creates, etc. Hopefully you can get this by querying system tables on your test server. An alternative is to export your test db, then delete all the *data* from that, leaving just the "structure". HTH Peter

        Software rusts. Simon Stephenson, ca 1994.

        1 Reply Last reply
        0
        • R Roger Wright

          I'll ask it anyway, having already tried Google and MSDN. I'm fiddling with a SQL Server app to track substation equipment - one of several projects I'm working on. I've got a skeleton working on a local instance of SQL Server Express, but I'm thinking ahead to the day I deploy this on the company server, which has the full version of SQL Server installed. I don't want to manually recreate the database on the server - that way madness lies - and I don't want to Export/Import my existing database, as it's full of test cases I don't want to have to delete. I'm sure there's a nice, efficient way to create a database in SQL Server programmatically when my app runs for the first time, but I haven't a clue how to do it. Google returns a plethora of responses, enough to completely confuse me, and MSDN returns none of any relevance - the usual ratio. Can someone point me to a tutorial of some sort that my aged brain can comprehend? [EDIT] While MSDN didn't help, I finally asked the right question in Google - which led straight back to Microsoft, of course:

          using System;
          using System.Data.SqlClient;
          String str;
          SqlConnection myConn = new SqlConnection ("Server=localhost;Integrated security=SSPI;database=master");

          str = "CREATE DATABASE MyDatabase ON PRIMARY " +
              "(NAME = MyDatabase\_Data, " +
              "FILENAME = 'C:\\\\MyDatabaseData.mdf', " +
              "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
              "LOG ON (NAME = MyDatabase\_Log, " +
              "FILENAME = 'C:\\\\MyDatabaseLog.ldf', " +
              "SIZE = 1MB, " +
              "MAXSIZE = 5MB, " +
              "FILEGROWTH = 10%)";
          
          SqlCommand myCommand = new SqlCommand(str, myConn);
          try 
          {
              myConn.Open();
          myCommand.ExecuteNonQuery();
          MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK,    MessageBoxIcon.Information);
          }
          catch (System.Exception ex)
          {
          MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
          }
          finally
          {
          if (myConn.State == ConnectionState.Open)
          {
              myConn.Close();
          }
          }
          

          Sorry about the formatting - that's Opera at work... The article further states that, if I want just to use the Model database, eliminate all the crud and use simply, str = "CREATE DATABASE MyDatabase". I haven't tried it yet as it's getting rather late, but I think I'll play a bit later this week. I presume that I can then use CREATE TABLE and all its sundry relative

          J Offline
          J Offline
          Jorgen Andersson
          wrote on last edited by
          #4

          It sounds to me that it's something similar to this[^] you need. It creates a script for recreating a database on another server.

          "When did ignorance become a point of view" - Dilbert

          R 1 Reply Last reply
          0
          • J Jorgen Andersson

            It sounds to me that it's something similar to this[^] you need. It creates a script for recreating a database on another server.

            "When did ignorance become a point of view" - Dilbert

            R Offline
            R Offline
            Roger Wright
            wrote on last edited by
            #5

            That's worth a look, Jorgen - Thanks! It just seems silly to me that I can't export my database to a XML file, then regenerate the database on the target server using that file. I've used db-based product before that create the database on installation, but I assumed there was a CREATE DATABASE function, in addition to the CREATE TABLE function used regularly. It never occurred to me that they were scripting a copy and import process in the installer.

            "A Journey of a Thousand Rest Stops Begins with a Single Movement"

            1 Reply Last reply
            0
            • R Roger Wright

              I'll ask it anyway, having already tried Google and MSDN. I'm fiddling with a SQL Server app to track substation equipment - one of several projects I'm working on. I've got a skeleton working on a local instance of SQL Server Express, but I'm thinking ahead to the day I deploy this on the company server, which has the full version of SQL Server installed. I don't want to manually recreate the database on the server - that way madness lies - and I don't want to Export/Import my existing database, as it's full of test cases I don't want to have to delete. I'm sure there's a nice, efficient way to create a database in SQL Server programmatically when my app runs for the first time, but I haven't a clue how to do it. Google returns a plethora of responses, enough to completely confuse me, and MSDN returns none of any relevance - the usual ratio. Can someone point me to a tutorial of some sort that my aged brain can comprehend? [EDIT] While MSDN didn't help, I finally asked the right question in Google - which led straight back to Microsoft, of course:

              using System;
              using System.Data.SqlClient;
              String str;
              SqlConnection myConn = new SqlConnection ("Server=localhost;Integrated security=SSPI;database=master");

              str = "CREATE DATABASE MyDatabase ON PRIMARY " +
                  "(NAME = MyDatabase\_Data, " +
                  "FILENAME = 'C:\\\\MyDatabaseData.mdf', " +
                  "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
                  "LOG ON (NAME = MyDatabase\_Log, " +
                  "FILENAME = 'C:\\\\MyDatabaseLog.ldf', " +
                  "SIZE = 1MB, " +
                  "MAXSIZE = 5MB, " +
                  "FILEGROWTH = 10%)";
              
              SqlCommand myCommand = new SqlCommand(str, myConn);
              try 
              {
                  myConn.Open();
              myCommand.ExecuteNonQuery();
              MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK,    MessageBoxIcon.Information);
              }
              catch (System.Exception ex)
              {
              MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
              }
              finally
              {
              if (myConn.State == ConnectionState.Open)
              {
                  myConn.Close();
              }
              }
              

              Sorry about the formatting - that's Opera at work... The article further states that, if I want just to use the Model database, eliminate all the crud and use simply, str = "CREATE DATABASE MyDatabase". I haven't tried it yet as it's getting rather late, but I think I'll play a bit later this week. I presume that I can then use CREATE TABLE and all its sundry relative

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              I agree with Mycroft. I detach the database, copy the files, and reattach.

              1 Reply Last reply
              0
              • R Roger Wright

                I'll ask it anyway, having already tried Google and MSDN. I'm fiddling with a SQL Server app to track substation equipment - one of several projects I'm working on. I've got a skeleton working on a local instance of SQL Server Express, but I'm thinking ahead to the day I deploy this on the company server, which has the full version of SQL Server installed. I don't want to manually recreate the database on the server - that way madness lies - and I don't want to Export/Import my existing database, as it's full of test cases I don't want to have to delete. I'm sure there's a nice, efficient way to create a database in SQL Server programmatically when my app runs for the first time, but I haven't a clue how to do it. Google returns a plethora of responses, enough to completely confuse me, and MSDN returns none of any relevance - the usual ratio. Can someone point me to a tutorial of some sort that my aged brain can comprehend? [EDIT] While MSDN didn't help, I finally asked the right question in Google - which led straight back to Microsoft, of course:

                using System;
                using System.Data.SqlClient;
                String str;
                SqlConnection myConn = new SqlConnection ("Server=localhost;Integrated security=SSPI;database=master");

                str = "CREATE DATABASE MyDatabase ON PRIMARY " +
                    "(NAME = MyDatabase\_Data, " +
                    "FILENAME = 'C:\\\\MyDatabaseData.mdf', " +
                    "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
                    "LOG ON (NAME = MyDatabase\_Log, " +
                    "FILENAME = 'C:\\\\MyDatabaseLog.ldf', " +
                    "SIZE = 1MB, " +
                    "MAXSIZE = 5MB, " +
                    "FILEGROWTH = 10%)";
                
                SqlCommand myCommand = new SqlCommand(str, myConn);
                try 
                {
                    myConn.Open();
                myCommand.ExecuteNonQuery();
                MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK,    MessageBoxIcon.Information);
                }
                catch (System.Exception ex)
                {
                MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                finally
                {
                if (myConn.State == ConnectionState.Open)
                {
                    myConn.Close();
                }
                }
                

                Sorry about the formatting - that's Opera at work... The article further states that, if I want just to use the Model database, eliminate all the crud and use simply, str = "CREATE DATABASE MyDatabase". I haven't tried it yet as it's getting rather late, but I think I'll play a bit later this week. I presume that I can then use CREATE TABLE and all its sundry relative

                C Offline
                C Offline
                Corporal Agarn
                wrote on last edited by
                #7

                It sounds like scripting is the best way as you do not want everything to go. However I do not know if Express has scripting. Another way would be to 1) backup the database 2) truncate tables 3) backup to second file then restore the second backup to new server.

                R 2 Replies Last reply
                0
                • C Corporal Agarn

                  It sounds like scripting is the best way as you do not want everything to go. However I do not know if Express has scripting. Another way would be to 1) backup the database 2) truncate tables 3) backup to second file then restore the second backup to new server.

                  R Offline
                  R Offline
                  Roger Wright
                  wrote on last edited by
                  #8

                  Actually, I found this[^] to be intriguing. I wonder if I can code this into a SqlCommand? :-D

                  "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                  1 Reply Last reply
                  0
                  • M Mycroft Holmes

                    Roger Wright wrote:

                    I'm sure there's a nice, efficient way to create a database in SQL Server

                    Not programatically but there are 2 very simple ways to move a copy of your database to the production server. You can either backup your express version and restore it to the server or take a copy of the .mdf and .ldf files to the server and attach them. I think the attach one has the benefit of bringing the diagram which the backup/restore misses. Both are VERY simple processes.

                    Never underestimate the power of human stupidity RAH

                    R Offline
                    R Offline
                    Roger Wright
                    wrote on last edited by
                    #9

                    Have you ever tried coding this[^] into a SqlCommand? I think I may give it a try...

                    "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                    1 Reply Last reply
                    0
                    • C Corporal Agarn

                      It sounds like scripting is the best way as you do not want everything to go. However I do not know if Express has scripting. Another way would be to 1) backup the database 2) truncate tables 3) backup to second file then restore the second backup to new server.

                      R Offline
                      R Offline
                      Roger Wright
                      wrote on last edited by
                      #10

                      I found the solution at Microsoft Support, not MSDN. :-D

                      "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups