Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Abosulute path to the connectionstring?

Posted on 2011-04-25
6
Medium Priority
?
380 Views
Last Modified: 2012-05-11
VS 2008
Windows forms

I am trying to make a getconnection method. Since I am using SQL CE I need an absolute path to my DB. I attempted to fill in my DB using the code I found off another forum here:
http://social.msdn.microsoft.com/Forums/en-IE/sqlce/thread/0b8a2c4f-8be7-40ee-9e54-d4a6f57a8f6f

Assembly.GetExecutingAssembly() is giving me an error saying it doesn't exist in this context
My code is below hep would be great!  
I provided my absolute path on the top in case it was needed.
public static SqlCeConnection GetConnection()
        {
            string connectionString =
               @"Data Source=C:\Users\J\Desktop\Final Project\prog03\BookStore.sdf";

            string applicationPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"prog03";
            if (!Directory.Exists(applicationPath))
                Directory.CreateDirectory(applicationPath);

            //build up the connectionstring
            string connectionString;
            connectionString = "Data Source=" + applicationPath + "BookStore.sdf";
            connectionString += "; Password=xxx";
            SqlCeConnection connection = new SqlCeConnection(connectionString);
            if (File.Exists(connection.Database))
                File.Delete(connection.Database);

            if (!File.Exists(connection.Database))
            {
                MessageBox.Show("Creating SQL database succeed!", "Testing");
            }
            return connection;
        }

Open in new window

0
Comment
Question by:linkcube1
  • 4
  • 2
6 Comments
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35461725
Hello, try with:
string applicationPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + @"prog03";

Open in new window

Or
string applicationPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"prog03";

Open in new window


If prog03 is a subdirectory you need change it to @"\prog03\"
0
 

Author Comment

by:linkcube1
ID: 35464232
ok, that leads to the debug folder, but when my program runs the database seems to disappear i dont know  if thats normal or not. my original path was @"Data Source=C:\Users\J\Desktop\Final Project\prog03\BookStore.sdf"; is there a way to lead directly to this path rather than the debug folder?
0
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 2000 total points
ID: 35464473
Hello, yes that behaviour is right, I recommend this:

1. Add BookStore.sdf to your project (I think this is already done)
 Step 12. Set the properties of BookStore.sdf like the next image.
 Step 2
When you run your project, your project DB is going to be copied to your application directory (debug / release), so your DB path must be set to your application path, you can use this code to make your connection string:
string connectionString = "Data Source=" + System.IO.Path.Combine(Application.StartupPath, "BookStore.sdf") + "; Password=xxx";

Open in new window


If you want your DB in a sub folder of your application folder, just create a New Folder inside your project and move the DB to it, when your run your project that folder will be created in your application folder output (debug or release), but remember update the last code to something like:
string connectionString = "Data Source=" + System.IO.Path.Combine(Application.StartupPath, "MyDbFolder\\BookStore.sdf") + "; Password=xxx";

Open in new window


Also, why you run this code?
            if (File.Exists(connection.Database))
                File.Delete(connection.Database);

            if (!File.Exists(connection.Database))
            {
                MessageBox.Show("Creating SQL database succeed!", "Testing");
            }

Open in new window


That will delete your DB.
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35464477
Ignore this text from the first line of my last post "yes that behaviour is right", Im referring to another thing.
0
 

Author Comment

by:linkcube1
ID: 35467860
ok, The way  have been running the program via "@"Data Source=C:\Users\J\Desktop\Final Project\prog03\BookStore.sdf";"

It writes to the DB and keeps it there, but whenever I read the DB from the debug or release folder in my program after I close out of it the program the items in the DB are gone again. My program does copy the DB to the debug/release  folder after each run but why are my items disappearing? I think the best solution is to make a path to just the path above if thats possible.
0
 
LVL 17

Assisted Solution

by:Carlos Villegas
Carlos Villegas earned 2000 total points
ID: 35470438
Oh sure, that is because you need to change that behaviour by setting the correct value in the "Copy to Output Directory"`property of your BookStore.sdf file, just set it to "Do not Copy", and copy it manually, in that way you can gain control over that file, and update it only when necessary.

I recommend you to keep using the dynamic data source path instead of the static one.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question