1

This is my First App with a Database.It installs and runs perfectly on my PC. But when installed on another computer,it throws an error:

System.Data.SqlClient.SqlException (0x80131904): 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.

What I need is, the setup file must self contain a database locally so that I can store some data and retrieve it.

The sample app is here :

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace form1
{
   public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }
    SqlDataAdapter da;
    DataSet ds;
    SqlConnection con;
    private void button1_Click(object sender, EventArgs e)
    {
        con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\sql.mdf;Integrated Security=True");
        da = new SqlDataAdapter("insert into STUDENTDATA(STUDENT,CLASS,SEX)values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')", con);
        ds = new DataSet();
        da.Fill(ds);
        MessageBox.Show("Registration has been successful");
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();

    }
  }
}

Note::The other computer has Visual Studio Installed in it.

Sreekanth Reddy Balne
  • 3,264
  • 1
  • 18
  • 44
  • 2
    Visual Studio being installed on database server is irrelevant. If you're going to learn data access, make sure you learn about [IDisposable](https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx). Storing data connections as fields is a *terrible* practice. You should keep their scope as limited as possible. Anyways, have you verified that SQL Server LocalDB is installed on the machine you intend to deploy to? – mason Mar 28 '17 at 18:34
  • 2
    SQL Server LocalDB is installed on the machine.I have checked it `sqllocaldb i` through command line. And the instance is V11.0 – Sreekanth Reddy Balne Mar 28 '17 at 18:41
  • Have you verified that the database file is in the correct location? – mason Mar 28 '17 at 18:42
  • 1
    It is within the project folder. – Sreekanth Reddy Balne Mar 28 '17 at 18:42
  • 1
    What happens if you replace `|DataDirectory|\\sql.mdf` with a physical hardcoded path to where the file is located? – mason Mar 28 '17 at 18:44
  • But the hard copied path : `C:\Users\SREE\Documents\form1\form1\sql.mdf` is specific for my own PC. I need it to run on client machine. – Sreekanth Reddy Balne Mar 28 '17 at 18:47
  • `(LocalDB)\\MSSQLLocalDB` this is the server right. From the error I could understand that there is no server instance. Could you please tell me how all those applications that require database run on client machines without knowing the server instances running or without even asking to install a Server to setup database? – Sreekanth Reddy Balne Mar 28 '17 at 18:49
  • Then hardcoding the path in the application code is not a good idea. That's what configuration files are for, like `app.config`. – mason Mar 28 '17 at 18:51
  • So what should I do now? Please help. – Sreekanth Reddy Balne Mar 28 '17 at 18:52
  • I just told you. Change it so that the path to your database file comes from an application configuration file rather than being embedded in the code. That allows you to change your configuration to suit the environment you deploy to. And will allow you to experiment. Although judging by the error message you've shown, it seems more likely that LocalDB isn't configured properly. I suggest you search similar [questions](http://stackoverflow.com/questions/26248293/sql-network-interfaces-error-50-local-database-runtime-error-occurred-canno). – mason Mar 28 '17 at 18:54
  • V11 is not the same as MSSQLLocalDB. Change connection string to use V11 or use sqllocaldb.exe to create an MSSQLLocalDB instance. – Crowcoder Mar 28 '17 at 19:11
  • Yes but how to know the version of a client machine? – Sreekanth Reddy Balne Mar 28 '17 at 19:18
  • If it is showing 11 it is almost certainly SQL 2012. Use `sqllocaldb.exe versions` – Crowcoder Mar 28 '17 at 19:20
  • I have to install my setup on different machines.Am I supposed to go on checking `sqllocaldb.exe versions` for each and every system? I need it automated. My setup must know the server instance of the target machine . Please provide with the code to check. @Crowcoder – Sreekanth Reddy Balne Mar 28 '17 at 19:26
  • Local DB is not really intended to be used outside of development. Have you considered SqlLite? It is a no-install database with MSSQL features including support from Entity Framework. – Crowcoder Mar 28 '17 at 19:30
  • Yes . I have tried to do so. But saw somewhere that there are some limitations compared to others. – Sreekanth Reddy Balne Mar 28 '17 at 19:40
  • I will give it a try. – Sreekanth Reddy Balne Mar 28 '17 at 19:40
  • I can't recommend SqlLite enough if you need an embedded database distributed with your application - it has no outside dependencies. You just need a NuGet package in your application and you're good to go. – mason Mar 28 '17 at 20:13
  • Yes. I need an embedded database distributed with my application. – Sreekanth Reddy Balne Mar 28 '17 at 20:17

1 Answers1

2

Try this, it worked for me.

Go to Tools/Options/Database Tools/click Data connection/Now remove Sql Server Instance Name from Right hand box/click ok.

Then try to add another database: Open Solution Explorer/Right click on project/Add New Item/Select Service-based database/click Add

enter image description here

Karen Gonzalez
  • 664
  • 9
  • 17