Release a WPF app with local database - c#

OK, so I am quite lost. This is my first real application written for windows with a local database.
I have written an WPF application in VS2012 with a local database, which has all CRUD written as stored procedures.
The app is working as I run it in VS. But as I build the app and install it (on any machine) the database is not accessible. I have a feeling the connection string is causing this issue.
First I used this connection string:
SqlConnection conn = new SqlConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename='D:\LH\Personalitytest win\DB.mdf';Integrated Security=True;Database=DB")
But as I got "Error: 26 – Error Locating Server/Instance Specified" I guessed that the Data Source of the connection is not correct (although the AttachDbFilename is definitly incorrect). So, I changed the connection string to:
SqlConnection conn = new SqlConnection(#"Server=(localdb)\v11.0;AttachDbFilename=" + AppDomain.CurrentDomain.BaseDirectory + "DB.mdf;Database=DB;Trusted_Connection=Yes;");
And I got an error which basically said that the file DB.mdf is not found.
So, I made sure that the .mdf and .ldf files are copied to the installation directory/database.
But after installation both these files are read only (even if these have read/write persmission as I am developing). I have tried to change the file permissions (for both files) as the application starts for the first time by:
string path = AppDomain.CurrentDomain.BaseDirectory + #"Database\DB.mdf";
FileInfo file = new FileInfo(path);
file.IsReadOnly = false;
and
File.SetAttributes(path, FileAttributes.Normal);
and
FileAttributes attributes = File.GetAttributes(path);
attributes = attributes & ~FileAttributes.ReadOnly;
File.SetAttributes(path, attributes);
but neither of these methods are working.
My question is basically, how should the database be implemented in the final release?

Your database should be located in your windows user directory
Other way is to install SqlServer Express on your machine. And configure your connection string accordingly.

Related

"Unable to open the database file". Do I have to put the full physical location of sqlite database when developing C# Console application

I have created a Sqlite database mydatabase.sqlite and placed it in my App_Data folder inside the console application project. I am getting an error:
Unable to open the database file
when developing a console application using sqlite and C#.
When I am using
string cs = #"Data Source=App_Data\mydatabase.sqlite;Version=3";
I am getting
Unable to open the database file error;
But when I am using
string cs = #"Data Source=C:\Users\UP_SW02\Desktop\ConsoleApplication1\ConsoleApplication1\App_Data\mydatabase.sqlite;Version=3";
The application is running properly.
Is there any possibility to use Sqlite Data Source not using full physical location?
Thank you..
All non-rooted paths used inside a program are relative to the folder in which the application is started. Note that this is not necessarily the folder that the application itself is in. You have to be very careful with such paths.
What you seem to be after is the folder relative to your own application. There's a simple method for getting that. If you have the Windows.Forms namespace included in your project, you can get the executable's path from System.Windows.Forms.Application.ExecutablePath:
// Get app folder
String appFolder = Path.GetDirectoryName(Application.ExecutablePath)
// Combine with sqlite db path
String dbPath = Path.Combine(appFolder, #"App_Data\mydatabase.sqlite");
// Build connection string
string cs = String.Format(#"Data Source={0};Version={1}", dbPath, 3);
An alternative for Application.ExecutablePath if you're not using a Windows forms program is Assembly.GetExecutingAssembly().Location, from the System.Reflection namespace.
The simplest way to get folder's relative path in a connection string is to simply add |DataDirectory|.
before
string cs = #"Data Source=App_Data\mydatabase.sqlite;Version=3";
after
string cs = #"DataSource=|DataDirectory|\App_Data\mydatabase.sqlite;Version=3";
No there is no way SQlite needs to full path address but you can resolve it with two solutions:
1- Save connection path in config file.
2- Get App_Data folder path by GetFolderPath method like:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
So your code would be like:
var fileName = Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData), "mydatabase.sqlite");
string sqliteConnection = string.Format("Data Source={0};Version=3;Timeout=2;", fileName);
3- Or the easy way is :
string sqlitePath = Directory.GetCurrentDirectory() + "/App_Data/mydatabase.sqlite";

Building a C# WinForms application with a local Db

I am trying to build my first .exe from a c# winforms project. I am using the Flexera Installing Shield. So far I can build and install it and it runs successfully on the same machine where I am developing. In this project I am using a local db. I can also install it on another machine, but as soon as I`m trying to access the Db via a button it complains. I think it has something to do with the connection string. At least it complains at the line where I am trying to access the Db with:
Error 26 - Error Locating Server/Instance Specified
Here is my obviously wrong connection string:
string connectionString = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=""C:\Users\idiot\Documents\Visual Studio 2013\Projects\Vis\Vis\LocalDbVisTest.mdf"";Integrated Security=True";
Thank you for any help or hint in advance!
Instead of using an absolute path for your connection string, use
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\LocalDbVisTest.mdf;Integrated Security=True
The reason your program cannot find the database is because it is looking in
C:\Users\idiot\Documents\Visual Studio 2013\Projects\Vis\Vis\LocalDbVisTest.mdf
Which, presumably, doesn't exist on your client's machine.
You can manually set your DataDirectory by using AppDomain.CurrentDomain.SetData("DataDirectory", path). You can get the path of your executable by using AppDomain.CurrentDomain.BaseDirectory

Connection string to access database in Program files

I am working on windows form which uses microsoft access database.
When the application will be installed, the database will be on this location
C:\Program Files (x86)\Amrit\trial\Database.
How can i make this work so that this application will linked to the database when installed in my computer.
Can somebody give me easy solution..
Currently my connection string looks like this..
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Amrit\\Desktop\\Database.accdb ;Persist Security Info=False;";
when i create setup file for appplication, the database will be in ProgramFiles/AmritCreations/AppName/Database.accdb
Just place your database and your application in the same directory and use this connectionString
string connString = "Provider=MICROSOFT.ACE.OLEDB.12.0; " +
"Data Source=|DataDirectory|/Database.accdb";
In times you're creating a Setup Project, try to place the database file and [yourProjectName].exe in the same directory.

Setting The Path of an db mdf file in local folder

How come The path I inserted in my Database Context is not working? Here's the code for my path
private string dbPath = #"TEST.MDF"
DataClasses1DataContext myDbContext = new DataClasses1DataContext(dbPath);
But when I run a query this gives me an error
An attempt to attach an auto-named database for file TEST.MDF failed. A database with the same name exists, or specified file cannot
be opened,
or it is located on UNC share.
And This is how My Folder looks like this
The mdf file is in the same location of my cs source code but the thing is they are not reading the path correctly.
my idea for this is that when I transfer to a diffrent pc I don't have to set up the paths again and again. are there any fix for this?
What about
private string dbPath = Application.StartupPath + "\\TEST.MDF";
But your Test.mdf isn't in the correct directory. Move it into \bin\Debug for this code to work.
Better you add your .mdf file in your project.. Add Existing Item=>Choose .mdf file from folder. After adding .mdf file in project, in Web.config or App.Config file connection string will be generate automatically and you can use that connection string to boot your store. Now as you build your project new .mdf file get copy into /bin/dubug folder and you dont need to write single line of code to connect your .mdf file.
Use this connection string :
SqlConnection connect = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename="+ AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.Length - 10)+"sampleDatabase.mdf;Integrated Security=True");
Because bin\Debug\ string length = 10, this is why we subtract 10; and now you can get the solution path address and can connect the MDF database.

Firebird client installation

I'm trying to deploy my Application that uses a Firebird v2.5 database to a client machine.
I am trying to use a minimum installation, without the need to run any other installers. From what I gather, all I need to do is copy the "FBClient.dll" to the target application folder (which includes firebird database file). I have tried this and it still reports an error about not being able to find the correct .net data provider.
eg
C:\Program Files (x86)\MyApp\myApp.exe
C:\Program Files (x86)\MyApp\fbDatabase.fdb
C:\Program Files (x86)\MyApp\fbclient.dll
The error produced is:
Failed to find or load the registered .Net Framework Data Provider.
Also have copied, renamed and included fbclient.dll as fbembed.dll
I have also tried to copy a bunch of other files to the app directory, as well as placing fbclient.dll into the c:\, c:\windows, c:\windows\system.
I also tried installing the client installation, with no joy too.
Is there a way, that I can use the firebird database, without manually editing the machine.config files or using the gac and going through the hell that I went through to install firebird on the dev machine? I want an application that a user can install, not requiring that it be installed by a developer.
Please note, the application that I am writing is for a single machine, single user environment, who knows how to double click the install button, with the attention span of a gnat, that if required to do more than double click install and then press GO, will simply get bored and press the cancel, forget it button.
I add the file "FirebirdSql.Data.FirebirdClient.dll" to the application folder and I no longer get the Data provider error, instead I get the following
"Unable to complete network request to host \"DevMachine\"."
at FirebirdSql.Data.Client.Managed.Version10.GdsConnection.Connect()
at FirebirdSql.Data.FirebirdClient.ClientFactory.CreateManagedDatabase(FbConnectionString options)
at FirebirdSql.Data.FirebirdClient.ClientFactory.CreateDatabase(FbConnectionString options)
at FirebirdSql.Data.FirebirdClient.FbConnectionInternal.Connect()
I am trying to connect with the following
string file = #"C:\Program Files (x86)\MyApp\Test.FDB;";
FbConnection con = new FbConnection("User=SYSDBA;" + "Password=masterkey;" + "Database=" + file + "DataSource=Dev-VS-W7VM;" + "Port3050;" + "Dialect=3;" + "Charset=ISO8859_1;");
try
{
con.Open();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
I have included FirebirdSql.Data.FirebirdClient in my project and installation folder (..\MyApp)
Thank you in advance.
You have to grap a zip and copy FirebirdSql.Data.FirebirdClient.dll to bin folder.
I wonder how you wrote your application that it works without the file (probably installed .msi) :)
You needed to install .net provider
http://www.firebirdsql.org/en/net-provider/
The database file resides next to your application's exe module. So, you need in local server running or embedded server in order to work with data. For embedded server a minimal set of files is (specifed relatively to an application folder):
\UDF (folder with UDF needed if any)
\Intl (with contents from FB installation)
fbembed.dll
firebird.msg
ib_util.dll
icudt30.dll
icuin30.dll
icuuc30.dll
Microsoft.VC80.CRT.manifest
msvcp80.dll
msvcr80.dll
for a full scale server (i.e. not an embedded) a list of files would be:
\UDF (folder with UDF needed if any)
\Intl (with contents from FB installation)
fbserver.exe or fb_inet_server.exe
fbclient.dll
firebird.msg
ib_util.dll
icudt30.dll
icuin30.dll
icuuc30.dll
Microsoft.VC80.CRT.manifest
msvcp80.dll
msvcr80.dll
security2.fdb
firebird.conf (if non default parameters used)
But then you would need in setting up service record for FB
or start it as an application before your application start.
Installation of the Firebird as a service could be done with
instsvc.exe utility. Appropriate commands:
instsvc install -s -a
instsvc start
Also you should take care of two things:
FileName must be a local file, not in a shared folder from another server.
Appending the firebird server IP before the filename. If you are running your app always in the firebird server, or you are using embeded fbclient.dll, than it should be like
FbConnection con = new FbConnection("User=SYSDBA;" + "Password=masterkey;" + "Database=localhost:" + file + "DataSource=Dev-VS-W7VM;" + "Port3050;" + "Dialect=3;" + "Charset=ISO8859_1;");
Some fbclient.dll version will allow you not to add localhost and still connect on local files, or use \\servername\c:\filename format instead of localhost:c:\filename, but it is deprecated and not supposed to work anymore (and can work depending on windows version).

Categories