Click to See Complete Forum and Search --> : Sql Compact, File Path problems


procopio
March 5th, 2010, 09:52 AM
Hi, here is a sample code. Please see the path to the datasource, it is very long, and if i will be deploying the application on other computer , the path would not be the same anymore.

SqlCeConnection conn = new SqlCeConnection(@"Data Source = C:\\Users\\Kiko\\Documents\\Visual Studio 2008\\Projects\\DelegaitAMS\\DelegaitAMSystem.sdf");
SqlCeCommand cmd = conn.CreateCommand();

cmd.CommandText = string.Format(@"INSERT INTO Office(office_name)" +
"Values('{0}')", txtbxOfficeName.Text);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

Now if I was to compile this code, and put the .exe on another PC, when I run it, it will be searching for that filepath for the db which wont be there :S But the DB will be, as it will be included in the program when it is compiled.

Basically, is there a generic filepath that will always link to the DB in the compiled code/program?

Please help me with my problem, i need this for my project.

nelo
March 5th, 2010, 10:39 AM
Please help me with my problem, i need this for my project.
:)

Sorry but I had to smile when I saw this. Nobody will be more inclined to help you just because you need it for a project (actually other students might...). You don't need to use such an argument. Most people on this forum are willing to help if they can ;). Now to your problem:

1. Put that information in the configuration file for application under the connectionStrings element (Probably the best solution). You can retrieve it using

// remember to reference System.Configuration.
string connectionString = ConfigurationManager.ConnectionStrings[<your key in here>].ConnectionString.

2. Use a relative path. If you don't specify the full path the operating syste will start by looking at the application directory (i.e. the directory where your executable is). For example under your bin folder you could have folder called 'Data'. You can put the file in there and in the code just say @"Data Source=Data\DelegaitAMSystem.sdf".

I think option 1 is the best.

procopio
March 6th, 2010, 09:37 AM
Thank you so much sir :)