0

I created a set up file and attached the database using SQL Server Express 2008, but when I run the app, I get this error:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server cannot be found or cannot be accessed.

Verify that the instance name is correct and that SQL Server is configured to allow remote connections.

(provider: SQL Network Interfaces, error: 26 - Error locating the specified server / instance)

I think it's the data source - is there anyway I can make my data source flexible?

Here is my data source :

SqlConnection cnx = new SqlConnection(@"Data Source=.\OSSAMA;Initial Catalog=assurance_stage;Persist Security Info=True;User ID=sa;Password=2041925");
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
ossama assaghir
  • 298
  • 2
  • 16
  • *"using sql server expreess 2008"* Why 2008 express? 2008 has been completely unsupported for a year, and upgrading to a supported version is completely free. As for your error, does the other PC have SQL Server installed, with the correct credentials and database set up? Also, *why* is your application using the `sa` account (and I hope that isn't your real password...). Use a `LOGIN` and `USER` with the **minimal** amount of permissions. – Thom A Jun 26 '20 at 13:04
  • is "OSSAMA" your pc or the one you installed the app on? – Crowcoder Jun 26 '20 at 13:04
  • Do you have SQL Server installed on that computer? – Alejandro Jun 26 '20 at 13:08
  • 1
    @Crowcoder OSSAMA is the instance name, not the computer, as it's preceded by `.\`, meaning that's an instance name on a local computer. – Alejandro Jun 26 '20 at 13:09
  • 2
    The data source of `.\OSSAMA` implies you have a named SQL instance installed on the local machine. So wherever you are installing the software, you also need to install the database server. – DavidG Jun 26 '20 at 13:09
  • Or instead of `.\OSSAMA`, use `\OSSAMA` to point at the one server. – Hans Kesting Jun 26 '20 at 13:13
  • @Alejandro thanks, that was dumb of me. – Crowcoder Jun 26 '20 at 13:14
  • i have sql server on the pc yes – ossama assaghir Jun 26 '20 at 13:22
  • i detached the database from my pc and atatched it in the other pc – ossama assaghir Jun 26 '20 at 13:22
  • *"i detached the database from my pc and atatched it in the other pc"* And did you create the needed `LOGIN` and `USER` account(s) on the other instance, or map the `USER` to the `LOGIN` correct;y? Like I said, I *hope* you're not really using the `sa` account, so you'll need to ensure that the credentials are correct if you've "copied" the database from one PC to another. – Thom A Jun 26 '20 at 13:36
  • Please do not edit solution announcements into the question. Accept (i.e. click the "tick" next to it) one of the existing answer, if there are any. You can also create your own answer, and even accept it, if your solution is not yet covered by an existing answer. Compare https://stackoverflow.com/help/self-answer – Yunnosch Sep 12 '22 at 10:30

2 Answers2

0

you need to communicate SQL server with your local IP and port number 1433 or whatever port using and make sure your computer connected to the local network. refer this issue How to connect to SQL Server from another computer?

Sanjeev Baria
  • 158
  • 2
  • 9
0

There is a workaround to check if your Database is accessible on your target machine

Create a text file and change its extension to .udl and double click it, it would be something like enter image description here

Put your credentials and test the connection, most of the times the DB is not accessible from the target machine, you may check if firewall is allowing the connection to DB

Naveed Yousaf
  • 436
  • 4
  • 14