2

I have a problem...i'm developing my first WPF app in Visual Studio...It's a simple login app with a local .mdf database. The app works perfectly in debug mode and works well when i publish and run it in my laptop. But, when I run it in another pc, the database connection fails. I use the |DataDirectory|, but seems that not working. This is the steps that I do for develop my simple app:

1) I create two texbox and one button;

2)I assign the action at the button...in particulary i write this code

SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database\Database1.mdf;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("Select * from Utenti Where Username='" + textBox.Text + "' and Password='" + textBox1.Text + " '", conn);
DataTable dt = new System.Data.DataTable();

3)Next I create in Solution explorer a folder called Database and into that folder I create a Database .mdf called Database1.mdf.

4)After that, I poupoulated it with fake data

5)I add the prerequisites "System" and "System.Configuration" at the project

6) I publish the app

What can i do? I spent a lot of effort, and i'm so confused...Thanks

  • Are you sure that `LocalDB` has been installed on the client machine? – David Tansey Oct 04 '15 at 21:02
  • It may also be that the instance name is different on the client machine. Try this `Data Source=(LocalDB)\v11.0;` in your connection string. More information is available here: https://connect.microsoft.com/SQLServer/feedback/details/845278/sql-server-2014-express-localdb-does-not-create-automatic-instance-v12-0 – David Tansey Oct 04 '15 at 21:05
  • Are you sure that the database file exists on the target system at all? You should find it in the output folder along with the exe and dlls. Check out this response too: http://stackoverflow.com/a/12276625/4424024 – Martin Oct 05 '15 at 08:32
  • Local db is installed because i insert in the prerequisites "SQL server express Local DB". Change the string in v11.0 not working...and yes, i think that the db is included in the project..if this can help, i share with you my application("Project.rar") and my source code that you can import in your visual studio ("LoginFinal.zip")...here: – Mirko Bastianini Oct 05 '15 at 09:19
  • https://drive.google.com/folderview?id=0B1DxAJcQcG_qbHZGS1VSYXRHMnc&usp=sharing – Mirko Bastianini Oct 05 '15 at 09:19
  • Perhaps you should perform a call to "File.Exists(pathToDatabaseFile)" before opening the database connection and check whether the database is in the expected location. Then you know it for sure. – Martin Oct 06 '15 at 14:50
  • The database is in the right place. At this point, i'm so confused...maybe @Martin you can tell me how can i correctly export and create a wpf application with a local database? Visual studio is very complicated with this feature...thank you :) – Mirko Bastianini Oct 07 '15 at 17:16
  • Usually you would write a installer project for this, because deploying application related files as is setup task. See here for more info: http://stackoverflow.com/questions/20351947/create-an-application-setup-in-visual-studio-2013 Perhaps you should try to configure the installer project so that it copies the database to the users "Documents" folder and let you application access the database from there. – Martin Oct 08 '15 at 07:37
  • Ok, i've write this code to see where is the datadirectory: – Mirko Bastianini Oct 09 '15 at 14:44
  • Ok, i've write this code to set and see in the visual app where is the datadirectory: AppDomain.CurrentDomain.SetData("DataDirectory",AppDomain.CurrentDomain.BaseDirectory); string x=(string)AppDomain.CurrentDomain.GetData("DataDirectory"); textBox2.Text = x; this code shows me where is the path of DataDirectory... i'm so confused because the .mdf file is present... – Mirko Bastianini Oct 09 '15 at 14:51
  • Finally...solved!!!!! For anybody having same problem...simply download from the microsoft site the SQLExpressLocalDB 2014.msi version, and include it in the setup file of your application....thank you all :D – Mirko Bastianini Oct 09 '15 at 19:04

0 Answers0