1

I'm working on a small project to track information. This C# application will take information uploaded via CSV/Excel and store/sort it.

My current connection string is an absolute path (off thumb drive). I'm worried that when I publish it, the database connection won't work on a random users computer.

            <add name="PPP_Project.Properties.Settings.Database1ConnectionString"
        connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Other PPP Projects\PPP_Project_Test\PPP_Project\Database1.mdf;Integrated Security=True;User Instance=True"
        providerName="System.Data.SqlClient" />

How do I have this set so it will work when the application is published?

Tim Stone
  • 19,119
  • 6
  • 56
  • 66
JoshG
  • 911
  • 3
  • 14
  • 20
  • Do you mean like using the app_data folder and loading the database from a local path using the ".\path" notation? – jcolebrand Apr 18 '11 at 21:19
  • Hey Drachenstern, I tried adding this folder, to test the non absolute path provided by Felice, but the Visual C# studio doesn't seem to have it. I also tried making it manually and putting the mdf in there. When running the code, nothing gets inserted into the database with the |DataDirectory| path, and no error is received. When I use the absolute path I provided, the data inserts no problem – JoshG Apr 18 '11 at 22:32
  • ~ His answer states "the same directory where the application is installed" so if you put it in app_data you have to update that to reflect a nested path. What happens if you do that? – jcolebrand Apr 19 '11 at 00:29
  • If I'm understanding what you're saying, I did try the relative connection string in with the MDF in both a made up folder in the project directory and a created (app_data) folder. The relative should work but nothing happens. When using a absolute reference to the database the program works every time. – JoshG Apr 19 '11 at 21:04
  • don't nest the mdf in a folder is what I'm saying. put it in the app execution path (so in the debug folder) – jcolebrand Apr 19 '11 at 21:46

2 Answers2

1

you can use this:

Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|Customers.mdf;Integrated Security=True;User Instance=True

as described here. For a winform application, the default |DataDirectory| is the same where the application is installed. Of course you can use aa a part of a more nested path. If you want to specifyng something else you can use the AppDomain.SetData method.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • Hey Felice, I have tried this method and when the connection string is set to this, the data does not get uploaded at all. It only seems to work when the absolute path is set. To clarify, this is an offline C# visual studio project. I'm not sure why the |Data Directory| doesn't work. – JoshG Apr 18 '11 at 21:58
  • Did you write DataDirectory *without* spaces ? – Felice Pollano Apr 19 '11 at 05:35
  • yes, sorry. I tried the exact format that you provided, and no luck. No error is returned but nothing happens. If I try an absolute path it works. – JoshG Apr 19 '11 at 21:05
  • well, pay attention that, for a winform application |DataDirectory| is the same place where the executable is, I guess you have the DB in the project dir instead. – Felice Pollano Apr 19 '11 at 21:08
  • Ah, I have checked the bin\debug folder and database1.mdf is in there with the .exe so that should apply and work correctly. It seems like this should be working in debug as well as when it's published. I'm at a loss. – JoshG Apr 19 '11 at 21:17
0

The easiest way to manipulate connection strings is with the ConnectionStringBuilder classes:

OleStringBuilder = new OleDbConnectionStringBuilder(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';");

OleStringBuilder.DataSource = MapPath(@"~\App_Datav\MyExcelWorksheet.xls");
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69