This May Be A Stupid Question But... [modified] Found It!
-
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 -
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 relativeRoger 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
-
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 relativeI 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.
-
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 -
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"
-
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 relativeI agree with Mycroft. I detach the database, copy the files, and reattach.
-
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 relativeIt 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.
-
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.
-
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
-
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.
I found the solution at Microsoft Support, not MSDN. :-D
"A Journey of a Thousand Rest Stops Begins with a Single Movement"