0

I can build an app using Windows Forms connecting to a SQL Server CE .sdf file to read and write. Now I need to do the same for a class library .dll project which intended to be a Revit Add-in so I added app.config and Settings.settings as well but in vain!

Any suggestion to properly save the connection string so it would work for multiple desktops?

Thanks in advance!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Just set the connection string in code?? – ErikEJ May 30 '16 at 16:09
  • Thank you @ErikEJ for Commenting. The problem in setting the connection this way -- String consting = "Data Source = |DataDirectory|RoomDB.sdf"-- is that it searches the "RoomDB.sdf" in the Directory where Revit is installed which off-course wouldn't be there. – Ibrahem Hamdy Naeem May 30 '16 at 18:28
  • I located the "RoomDB.sdf" in the same directory of the project-of the add-in- and I need the connection String to reference to that location. Is that possible? – Ibrahem Hamdy Naeem May 30 '16 at 18:31
  • In other words, I need the connection string to be formatted in such a way that it doesn't depend on my PC, I need it to be in a format that enables my add-in to be installed in multiple PCs without loosing the connection with the .sdf file. Please help Mr. @ErikEJ! , and to forget Thank u @marc_s for correcting my post. – Ibrahem Hamdy Naeem May 30 '16 at 19:02

1 Answers1

0

If you use DataDirectory, you can set the value of that in code:

string path = // folder path to the database file       
AppDomain.CurrentDomain.SetData("DataDirectory", path);
ErikEJ
  • 40,951
  • 5
  • 75
  • 115
  • Would this succeed if I try to install the addin in other PCs? – Ibrahem Hamdy Naeem May 31 '16 at 15:25
  • I tried to reference to the same dll directory using the following format : String mycon = " Data Source = \"System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)\\RoomDB.sdf"; but it gives me the following error "Format of initialization string does not conform to specification starting at index 1." – Ibrahem Hamdy Naeem May 31 '16 at 15:28
  • To your question - yes, if you are able to locate the .sdf file! – ErikEJ May 31 '16 at 15:30
  • alright , I'm afraid if I do this , Revit would have the current Data directory as the path I provided and then I will come up with endless errors of not finding its own .dll files like RevitAPI.dll – Ibrahem Hamdy Naeem May 31 '16 at 15:33
  • Yes, Surely. I have studied C# Fundamentals & SQL Basics with some background from the university but I love programming so recently I'm bracing my practice with the internet research and asking questions I don't know like this one. Do you have an Online C# or SQL Courses that I can follow? – Ibrahem Hamdy Naeem Jun 01 '16 at 06:44