0

I have a c# program that connect with a connection string to a db. I want to copy the program in another pc and copy .mdf file in debug folder. When I use

Data Source=.\\SQLEXPRESS;initial catalog=test;Integrated Security=true;

I have error like

SQL Network Interfaces, error: 26 – Error Locating Server/Instance Specified

in other pc.

in the other hand when I change the cstring like this

Data Source=.\\SQLEXPRESS;AttachDbFileName=|DataDirectory|test.mdf;Integrated Security=true;

I have this error in my visual studio

Database '{0}' already exists. Choose a different database name. Cannot attach the file '{0}' as database '{1}'.

Really I dont know how fix this error help !

Is it necessary to have a server name SQLEXPRESS and a db name test in other pc ??

James Z
  • 12,209
  • 10
  • 24
  • 44
amir.wpf
  • 31
  • 2

1 Answers1

1

If You want the application to connect to a DB in a *.mdf file, it seems that some kind of SQL server installation is required on a PC that wants to process the file. Probably this can be either a local SQL server or a remote one.

The dot . in the in the Data Source part of the connection string means that You refer to the local computer where the application is running. So, when You move the application to another PC and leave Data Source as .\\SQLEXPRESS, You refer to SQLEXPRESS SQL server instance on that other PC. It probably doesn't have SQL Server installed (or has got another SQL instance name), which causes the error You described.

So, basically, one way to fix the issue is by installing some form of SQL server on the other PC, if it's possible. For SQL Server 2012 it may be possible to use a special version of SQL Server runtime available as a separate download from Microsoft's sites. For more information and additional explanation, refer to this answer on SO.

If You don't want to install the SQL server locally, the issue may also be fixed by using a remote SQL server on another machine. In this case, the dot . should be replaced with name of a machine where the SQL Server resides and that is accessible to that other PC.

Community
  • 1
  • 1
Lukasz M
  • 5,635
  • 2
  • 22
  • 29
  • thank u my friend ! i installed a instance name SQLEXPRESS in that pc but still i have same error. what else can i do? – amir.wpf Jan 05 '16 at 16:34
  • First, try to specify not only the file name, but also database name in the connection string using 'Initial catalog' setting. If this doesn't solve the issue, take a look at this solution http://stackoverflow.com/a/16091134/232530 – Lukasz M Jan 05 '16 at 23:40