I have just compiled and installed an application that generates and uses an SQLite database.
In my App.config file, my connection string is like this:
<add name="DBContext" connectionString="data source=.\DB.sqlite" providerName="System.Data.SQLite" />
In my debug/release, I can see that the DB.sqlite file is created in the same folder.
However, when I have installed the compiled application, it goes to a directory called:
C:\Users\uua\AppData\Local\VirtualStore\Program Files (x86)\App
Is there a way to make sure the database appears in a custom location or in the same folder as my installed application?
Is there a way to make sure the database appears in a custom location or in the same folder as my installed application?
You could always use an absolute path:
<add name="DBContext" connectionString="data source=c:\folder\DB.sqlite" providerName="System.Data.SQLite" />
The obvious drawback with this is that you need to make sure that the directory actually exists on the machine(s) where your app is installed.
You may want to consider setting the connection string programmatically.
Related
I recently finished building this C# Winforms app that uses a localDB connection and when installing it on another PC, I get the error as shown in the title, even though I included the SQL Server Express localDB in the prerequisites folder.
I tried all kinds of solutions like changing the method of how to build the setup file (using Advanced Installer), but it seems that the problem is always related to the constant AttachDbFilename attribute of the connection string that's not changing according to where the database is newly installed.
Here's how the connection string is defined in the App.config file:
<connectionStrings>
<add name="client"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Surface\source\repos\VisaTurbo\VisaTurbo\Client.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Please I've been stuck with this problem for 3 days now and the due date to submit the application have already been reached.
Thanks in advance
You can by example check the database file exist in %userProfile%\DBName.mdf and create a new one with https://github.com/martincostello/sqllocaldb.
But this make the connectionstrings parameters obsolete :)
You can also generate a new one with SqlConnectionStringBuilder
But if the user move the database file this will reset the application each time.
That's why in general the database in on a server, not locally stored on the user's machine.
I am very new to SQL and tried to build a software by using SQLServer and Visual Studio. I created a setup file for my program by using InstallShield Limited Edition Project. I want my program to be used on other computers and I want every user to be able use their own databases installed on their computers. To me, when a user installed the program, the program will search for a connection string that I used while creating the program. Therefore, I think this connection string must be changed by users. How can I add such properties into my program? By the way, I used model first entity framework in my program. My connection string written in app.config is :
<connectionStrings>
<add name="otobusVTNesneleri" connectionString="metadata=res://*/OtobusVeriModeli.csdl|res://*/OtobusVeriModeli.ssdl|res://*/OtobusVeriModeli.msl;provider=System.Data.SqlClient;provider connection string="data source=PIPASO\PIPASOSERVER;initial catalog=otobus;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
P.S. I searched the internet about this problem and saw that people suggest that I use SQL Server compact. I want to solve this problem without using it. I have SQL Server 2012.
You shouldinstall the SQL Server with the same instance name in all the PCs. And then, in the connection string, instead of specifying the computer name, use the "local" syntax:
Data Source=(local)\yourInstanceName
It's not a good idea to let your users change the connection string.
Entity Framework starting from version 6 supports Code Based configuration. Refer to this article: Code-Based Configuration (EF6 onwards)
without use sql server username password use it.
<configuration>
<connectionStrings>
<add name="ConString" connectionString="Data Source=Trainee4-PC;Initial Catalog=Demo05-09-2014;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
use sql server username password use it.
<configuration>
<connectionStrings>
<add name="ConString" connectionString="Data Source=DENISH\SA;Initial Catalog=Demo05-09-2014;User ID=Demo;Password=Demo123" />
</connectionStrings>
</configuration>
local databases should be sql compact (sdf files). in any case, what you need to do is to make the installer modify the app.config file after it's deployed. this is what i'm doing in my current project. in my current project i have a local database (an sdf file) and an installer (created using WiX) that deploys my app. i configured the installer to modify some app.config settings after deploying the app. one of the settings was the connection to the local sdf database.
one thing to be careful about is that, while using sdf files, you cannot specify relative file paths in the config file. you need to either specify the full path (ie: d:\..\db.sdf) or use special folder names (ie |AppData|db.sdf). AppData will resolve to the current path of the executable.
requiring a full blown sql database on the client will also mean you'll have to install SQL Express on the client machine. you don't have to do this with sdf files.
in case you decide to use WiX you can modify your connection string like below.
<util:XmlFile Id="WindowsServiceUpdateConnectionString" File="[INSTALLFOLDER]$(var.Phoenix.WindowsService.TargetFileName).config" Action="setValue"
ElementPath= "//configuration/connectionStrings/add[\[]#name="PhoenixCacheEntities"[\]]/#connectionString"
Value="Data Source=[INSTALLFOLDER]Cache\PhoenixLocal.sdf"/>
you can see that an xpath is used here to determine the setting you want to change. hope this helps.
NOT RECOMANDED
its just becouse you said you are just starting in c#
so to write the connection string into a file :
using System.IO;//delcare it the top
File.Write("config.cfg","your connection string goes here");
to read From it :
using System.IO;//delcare it the top
string CNXSTRING = File.Read("config.cfg");
btw you have to create a form where the user and can type the new connection string lets say it have a textbox named txtstring the code will become
File.Write("config.cfg",txtstring.Text);
PS : use the write when you want to save the CNX String like in the save button of that form .
I'm trying to use an 'SQL Express' (or is it SQL Compact?) .MDF file as the datasource on a simple website using a Chart control.
Locally, it works great; but only if the 'AttachDbFilename' has the full path. Am I messing something up, or does it really need to be full path? If so - how do I accomplish this without using something like Server.MapPath()?
<add name="LocalSqlServer"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
By default, the |DataDirectory| variable will be expanded as follow:
For applications placed in a directory on the user machine, this will be the app's (.exe) folder.
For apps running under ClickOnce, this will be a special data folder created by ClickOnce
For Web apps, this will be the App_Data folder
You can programmatically set DataDirectory by calling AppDomain.CurrentDomain.SetData("DataDirectory", newpath)
I have a system comprising 5 applications. Each app accesses a database via a DAL library. In the DAL i have an app.config with the following entry:
<connectionStrings>
<add name="DataAccessLayer.Properties.Settings.ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\something\something\MyDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Using the full path for the attachDbFilename works fine. But I'm not happy:
I have copied the app.config file into each app that uses the database. Best way of doing this - copying the DAL app.config as a Link in the other projects?
I dont want a full path, when it comes to deployment that aint going to work. Relative paths in the app.config do not appear to work. Ideally I would like to be able to pull the DAL from source control onto any computer and not have to worry about changing the connection string each time. This: http://blogs.msdn.com/b/smartclientdata/archive/2005/08/26/456886.aspx talks about |DataDirectory| for deployment purposes but this doesn't work for me in debug (unless I'm using it wrong, see 3)
This might be better as a separate question but it is related to 2. - Is there a "good" way of arranging multiple projects for debug? I have created a Bin dir and in each project settings I copy the dll/exe to this bin dir. I also have a copy of the database in here (I tried no path in the app.config but that didn't work either, nor did |DataDirectory|). Also incredibly annoying is that relative paths do not work in Debug\Working Directory setting either, so it looks like that is one place that would have to change each time code is checked out to a new machine?
Apologies for the war and peace and thanks in advance for any ideas.
Two answers - but not really full solutions:
1) I have copied the app.config file into each app that uses the database. Best way of doing this - copying the DAL app.config as a Link in the other projects?
You could externalize the connection strings into their own config, something like:
<connectionStrings configSource="connectionStrings.config" />
and then have those connection strings in that new file:
<connectionStrings>
<add name="DataAccessLayer.Properties.Settings.ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\something\something\MyDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" />
</connectionStrings>
That way, you can have custom app.config's, but share the commonality.
2) I don't want a full path, when it comes to deployment that ain't going to work. Relative paths in the app.config do not appear to work.
Unfortunately, the only thing you can do here is to use the |DataDirectory| placeholder, which is a placeholder for the App_Data folder in an ASP.NET application.
<add name="DataAccessLayer.Properties.Settings.ConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|MyDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
providerName="System.Data.SqlClient" />
The only other solution would be to use a server and connect to a server - rather than having files to dynamically mount and attach.
Using the connection string AttachDbFilename feature from multiple processes referring the same MDF is very bad idea. Sharing an auto-atached database can get you in all sort of complicated security/ownership database startup problems. And specifying User Instance=True is like pouring gas over the flame, since each user instance is per user so if your applications are ever configured to run under different apppool credentials or one suddenly is changed to impersonate, all hell breaks loose.
Just attach for good the MDF as an normal database to your SQL instance and use it as such, with a normall connection string: Data Source=.\SQLEXPRESS;Initial Catalog=<dbname>; Integrated Security=True.
I created the setup project for the application and I can see that the later modifications of the configuration file (Application.exe.config) don't affect the application execution.
I am developing an application with the database file included and I want to enable users to move the database file and modify connection strings.
Does anyone know what's the best practice for the deployment of the application with the database file?
It should work, provided that you use the exact same connection string setting in your DB access DLL's Settings.settings file and in your application's config file.
An example that works well for me:
<connectionStrings>
<add name="YourApp.Properties.Settings.DatabaseConnectionString"
connectionString="Data Source=localhost;Initial Catalog=xxx;Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
When entered appropriately in both locations (ie. the dll's Settings.settings and the exe's App.config files), this does allow me to change the database connection in YourApp.exe.config before the app runs.
(I assume you already know that you need to change the application's config file, as DLL's do not support the app.config mechanism directly.)
Have you checked out using a UDL file?
If you save your connection string in the udl file, the user can change the connection via an interface by simply double clicking that file.
You can set your connection string in the app to point to the udl file.
You can also launch the udl interface programmatically if you want.
The only downside to these is if the user saves their password to the file, it is saved as plain text. But this isn't an issue if you are using windows authentication.
did you make sure to remove the settings default values? These are compiled and fetched from the dll and not from the config file.