|
-
March 5th, 2010, 10:52 AM
#1
Sql Compact, File Path problems
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.
-
March 5th, 2010, 11:39 AM
#2
Re: Sql Compact, File Path problems
 Originally Posted by procopio
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
Code:
// 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.
Last edited by nelo; March 5th, 2010 at 11:42 AM.
-
March 6th, 2010, 10:37 AM
#3
Re: Sql Compact, File Path problems
Thank you so much sir
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|