Set the directory (connection string) for an SQL Server Database Connection


/ Published in: C#
Save to your folder(s)

This snippet is actually tested for SQL Server Compact Edition (CE) 3.5, but it will probably work for other SQL Server editions. Once you have an SQL CE db up and running, you can set the connection string to point to any directory and database by simply using the |DataDirectory| string in the dbConnectionString Property (created by default when you create the database connection) and then relocating the location to which |DataDirectory| points by calling AppDomain.CurrentDomain.SetData("DataDirectory", @"C:\Path\To\DB"). Since the dbConnectionString property appends \dbName.sdf, we leave that part off of the SetData argument.

The code below sets the data directory to the common application data folder (usually C:\Documents and Settings\All Users\Application Data on winXP). It seems to work fine when invoked in the Form_Load method.


Copy this code and paste it in your HTML
  1. Form_Load(object sender, EventArgs e)
  2. {
  3. // Set the db directory to the common app data folder
  4. AppDomain.CurrentDomain.SetData("DataDirectory", System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData));
  5. }

URL: http://social.msdn.microsoft.com/forums/en-US/sqlce/thread/dc31ea59-5718-49b6-9f1f-7039da425296/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.