0

I am writing a Windows Forms application, where I want to save data inside the application. For the reason I used a serviced-based database with a .mdf database file, and I use Entity Framework.

So far everything is working fine until I released the file and opened it on another computer. The application opened and everything worked fine but when it comes to the database interaction, it throws a big error like this:

************ Exception Text ************
System.Data.Entity.Core.EntityException: The underlying provider failed on Open.

System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.)

Here a screenshot of the error: https://www.dropbox.com/s/gwban6ab97c6fya/22093543_10213995193571435_760919436_n.png?dl=0

In case you need the project its uploaded to here: https://www.dropbox.com/s/ubpc683ggtihh6k/usercontrol.zip?dl=0

I have tried in so many ways but nothing is working, same thing happens for the installer version as well.

Can anyone help me on this please?

Here are my connection strings:

<add name="DatabaseEntities" 
     connectionString="metadata=res://*/dbModel.csdl|res://*/dbModel.ssdl|res://*/dbModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\Database.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;"  
     providerName="System.Data.EntityClient" />
<add name="OPLCheque.Properties.Settings.DatabaseConnectionString" 
     connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True" 
     providerName="System.Data.SqlClient" />
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
saydur rahman
  • 119
  • 1
  • 9
  • 1
    Based on the error you are using `LocalDB`. Check your connection strings and make sure you are not using a local database if you intend to share database between different users. – jegtugado Sep 28 '17 at 05:02
  • Thanks for your comment. I have updated my question with my connection string that is not working on other pc. but same release is working on my pc. Can you tell me any solution on that please @Ephraim – saydur rahman Sep 28 '17 at 05:07
  • i also have the same in entity framework in wpf application did you find any solution ? – Ahmad Mar 03 '18 at 08:47

1 Answers1

0

I'm pretty sure this post would be flagged as duplicate since a similar question has been answered multiple times already, but here you go.

  • You are using LocalDB which means it cannot be shared since it runs on SQL Server Express.

If you simply want a LocalDB, see this answer to implement it after publishing your WinForms app.

Basically you want to publish your app with prerequisite components so you should publish those along your application.

  • If you want to use a single database to be shared between instances of your application, learn how to setup one using SQL Server here.

Afterwards, learn what connection string format you should use.

Sample to connect via IP Address:

connetionString="Data Source=IP_ADDRESS,PORT;
Network Library=DBMSSOCN;Initial Catalog=DatabaseName;
User ID=UserName;Password=Password"

See this for more.

  • If ever you plan on using a shared database, better make sure that the connection string that your database context should use is specified.

Config:

<add name="DefaultConnection" connectionString="" providerName="" />

Database Context:

public DbContext() 
    : base ("DefaultConnection") 
{
}
jegtugado
  • 5,081
  • 1
  • 12
  • 35
  • I am not using a shared database.Andi dont want to connect my application with any other ip.I just want to keep things inside the application.No other database.When user will install the application the storage will be created according to that and save and extract data from there. Is service based databased ok for my purpose or otherwise? I repeat my purpose is not to use any other database i need my data to keep inside the applications file and folders. and i need to use entity – saydur rahman Sep 28 '17 at 05:39
  • I linked the one you need in my answer. Please check. – jegtugado Sep 28 '17 at 05:40
  • I tried with the prerequisites also. but it shows the following error while installation.https://www.dropbox.com/s/n1ntds0g785s3zk/22091864_10213995389016321_1870437363_n.png?dl=0 – saydur rahman Sep 28 '17 at 05:57
  • Uninstall your application and re-install it using the installer from the publish location. – jegtugado Sep 28 '17 at 05:59