I am writing a program in Visual C# 2015. I have created a local database from within the IDE and everything seems to work fine after building and running locally. However, after publishing the application and installing on another machine I get an argument exception error at attachdbfilename. I have included the database as a content file. The ClickOnce does copy the database to a folder under APPS in my Appdata folder. I have installed SQL Local DB edition on the target machine. The database name is ConnectionsDatabase.mdf.
These are definitions in my Settings.Designer.cs file.
[global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
[global::System.Configuration.DefaultSettingValueAttribute("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\ConnectionSet" +
"tings.mdf;Integrated Security=True; User Instance=True")]
[global::System.Configuration.DefaultSettingValueAttribute("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\ConnectionsDa" +
"tabase.mdf;Integrated Security=True; User Instance=True")]
Not sure if they should both be there for just 1 database.
This is what I use for connection string within the appropriate classes.
using
(SqlConnection con = new SqlConnection(Store_Switcher.Properties.Settings.Default.ConnectionsDatabaseConnectionString))
I am willing to share any and all project files that are needed. Keep in mind this is a small application that will be installed on several different machines. Please help!! This is driving me crazy!
AttachDbFilename=|DataDirectory|\ConnectionSet" + "tings.mdf
should be
AttachDbFilename=|DataDirectory|\ConnectionData" + "base.mdf
in your designer file. or vice versa. you have a disconnect in file names.
EDIT:
You mean "invalid value for key"? The problem sounds like whatever path you are using for the database on the target system doesn't match your development machine.
You even say "The ClickOnce does copy the database to a folder under APPS in my Appdata folder. " So maybe on your dev box you have c:\myapp\App_Data\ConnectionData.mdf and on target it is c:\myapp\App_data\APPS\ConnectionData.mdf after it gets deployed.This won't work. Get rid of the APPS, or change your code so the deployment folder is the same as on your dev box. This is a path issue.
Related
I have a CRUD application using a local database (SQLite). My problem regarding saving this database is as follows:
When I use the following code:
string dbPath = Path.Combine(FileSystem.AppDataDirectory, "Student.db3");
_dbConnection = new SQLiteAsyncConnection(dbPath);
works fine when testing the app on a windows machine and I have found the file here:
C:\Users\{myUsername}\AppData\Local\Packages\7BD31CA9-CFFD-4A21-9B24-A87481C6221D_9zz4h110yvjzm\LocalState
But when running this app on the android emulator, I have no idea where it is saved... I have searched the AppData directory back and forth and used the searching functionality of the directory, but nothing.
From anyone's experience, how can I save this database file in the current directory of the project itself to ease things up?
Notes:
I have tried using this path, to save the file in the current directory, but nothing when testing on the Android emulator.
AppDomain.CurrentDomain.BaseDirectory
It just seems to me that, the file is saved to a completely different when tested on the Android emulator but don't know where. Thoughts?
Is there any way to have the local database on the desktop machine, in the project's directory? Knowing that the database is in the android file system.
According to the documentation:
Returns the FilesDir of the current context, which are backed up using Auto Backup starting on API 23 and above.
Depending on how you deploy the app to the emulator, it should be in the same directory as binaries for the app.
See File system helpers - Platform differences
I have a C# application that creates an installer with a Setup project in Visual Studio. It all works fine except for the SQLite database that I am trying to include.
What I have done:
Added the database file from my solution to the Application Folder of the Setup project.
Stored the connection string in my code where I use it to create the connection.
private const string conString = "DATASOURCE = HCATDatabase.sqlite; VERSION = 3;";
SQLiteConnection dbConnection = new SQLiteConnection(conString);`
Rebuilt the Setup project and the Solution.
Installed the program from the created installation.
What happened when I fired the installed application:
The database gives errors like "No such table", while the database in my solution does have these tables.
The application shows data that is not in the database from my solution.
I compared the database files with DB Browser (the one from my project that I included in my Setup project and the one in the installed program's file) and they are identical.
So somehow the program uses a different (third) database file.
How can I find out where the program gets this file from and how can I change it to use the proper file?
UPDATE: I have found thanks to #Roger Lipscombe that the program uses a database file in C:\Users\User\AppData\Local\VirtualStore\Program Files (x86)\ProgramCompany\MyProgram.
UPDATE 2: I deleted the folders (that were pretty old) in the above directory and that fixed everything, the program now references the correct database. I would love it if someone could explain all of this.
I created a Windows form applicatie with a local database (.mdf) to store and retrieve data from.
the database where I connect to is:
C:\ProgramData\project\Database.mdf
when I publish my project and place my database file in that folder on a other pc and try to run it I get the error unable to locate a local database runtime installation
my connection string is:
conn.ConnectionString = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=""C:\ProgramData\project\Database.mdf"";Integrated Security=True";
so could somebody help me with this problem?
because everything runs fine on my own pc
Did you include the database as "Application File"? If not do the following (at least this is how I am doing it):
Project -> Properties -> Publish -> Application Files
Here set the values for your .mdf and the xx_log.ldf as follows:
Now still in the Publish tab go on Prerequisites. Here you have to check the following depending on what database you are using.
This will download SQL Server Express for the client who is installing your application.
You will also have to change the connection string to a generic path. I suppose the database lies somewhere inside your project folder /bin I guess, not sure anymore. So adjust your connection string to something like:
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True
I therfor recommend using a resource file or app.config
But basically i think your problem is that the pc you are installing on does not have SQL Server installed. So just follow the steps above in Prerequisites. The other steps will enable you to deploy the database to the project folder without moving it to a certain folder manually.
I hope this helps.
In a project, I have a local database, (I am using the datagrid to view the data), but the problem is that when I insert into database using the insertion query, the insertion is not lasting, i.e. when I rerun the application, the database is not changed and the changes are not reflected in the database view (view table data in Visual Studio).
The insertion query is:
string dbCon = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\MyDatabase.sdf";
SqlCeConnection dbconnection = new SqlCeConnection("datasource=" + dbCon);
dbconnection.Open();
String Insert = "insert into MainTable(LineID,Date,Connection_Type) Values('5','5/8/2012','commercial')";
SqlCeCommand cmd = new SqlCeCommand(Insert, dbconnection);
//// Execute the command
cmd.ExecuteNonQuery();
dbconnection.Close();
Now when this code runs, the datagrid is updated, but the changes are not permanent. What is it that I am doing wrong?
I am using Visual Studio 2010 Ultimate and Windows 7
In what folder is your application running? If it is in the Program Files tree you likely do not have write permissions to the sdf file (especially on Windows 7 unless you have turned off UAC or are elevating your app prior to execution).
Even if that is not your short term issue I would recommend finding a different place for the sdf file, such as one of the AppData locations. Expecting to be able to write to your install location is not a guaruntee and depends on how and where your app is deployed.
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\MyDatabase.sdf" is the path to your application folder. Depending on installation options your file may not be installed there. Use a connection string of "DataSource=|DataDirectory|\\MyDatabase.sdf;..." as described here.
Even if your data is actually stored there, depending on user permissions the user may not be able to write to that directory. Also, when the user uninstalls or updates your app, his data will be deleted. People don't like that. You should copy the data to a user-writable folder; the earlier link shows how to do that as well.
If you have a Visual Studio project with a database a part of your project, it will probably have the "Copy to Output Directory" property set to "Copy". That means each time you run in the debugger you get a brand-new database copied from your source project, overwriting whatever was there before. Any changes will be in this temporary output folder, not in your original source data. When you debug the app again, the temporary copy is deleted. Further explanation in this answer.
I am working on c# windows project.I am using Microsoft Access database with OleDbConnection connection.My database "email.mdb" is in My document directory and my "email_db.udl" is in "D: drive".This is running well on my computer.But whenever i am making an exe installer file for installment on other Pc this is not working.I am placing "mdb" and "udl" file is in same directory as they are on my PC.I am supposing this ('udl' file) is not connected with my database.
How will i resolve this problem for installation on any windows pc.
Thanks
You can put the database files in your project's directory (where sln file is) and access them through their bare filenames (without D:...., only filename).
In case of the installation you have to set the installer to put it in the installation folder (where the exe file is)
This is a good way to carry everything in one folder and be organized.
Probably here you have a problem because the "My Document" directory has a different path in every PC.