July 27, 2008 at 11:40 pm
I create a db as follows:
string sqlConnectionString = "Data Source=.\\SQLEXPRESS;" +
"Integrated security =SSPI;";
SqlConnection connection = new SqlConnection(sqlConnectionString);
string sqlText2 = "CREATE DATABASE Development ON PRIMARY " +
"(NAME = development, " +
"FILENAME = 'C:\\development\\sql2005\\development.mdf') " +
"LOG ON (NAME = development_log, " +
"FILENAME = 'C:\\development\\sql2005\\development_log.ldf')";
When I check using Management Studio everything looks OK. "development" is listed under the database heading. So far, so good.
Then I create a table as follows:
string sqlConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\Development\sql2005\development.mdf;Integrated security =SSPI;";
SqlConnection connection = new SqlConnection(sqlConnectionString);
//Create States table
#region
//DataTable dt = new DataTable("tbl_state");
string createState = "CREATE TABLE tbl_state " +
"(" +
"stateCode char(2) NOT NULL," +
"stateName char(20) NOT Null)" ;
SqlCommand cmdCreateState = new SqlCommand(createState, connection);
connection.Open();
try
{
cmdCreateState.ExecuteNonQuery();
}
When I go to Management Studio I now have two entries under Database:
c:\Development\sql2005\development.mdf
development
I don't know why this is happening! Anybody have any ideas/suggestions?
TH
July 28, 2008 at 10:01 am
The attach command is a way to attach data and log files to a sql server. So you are basically telling it to attach a new database. Attach is not connect.
Connect to the database you created. Then issue a SQL statement with the create table command.
July 30, 2008 at 3:26 am
SqlConnectionStringBuilder.AttachDBFilename Property
Gets or sets a string that contains the name of the primary data file. This includes the full path name of an attachable database.
Atif Sheikh
July 31, 2008 at 8:38 am
Thanks for the help. Problem solved!
Appreciate your time and knowledge.
TH
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply