[英]Error in connect to database with connection string AND error to attach a db file
I have ac# program that connect with a connection string to a db
. 我有一个ac#程序,通过连接字符串连接到
db
。 I want to copy the program in another pc and copy .mdf file
in debug folder. 我想将程序复制到另一台PC中,然后将
.mdf file
复制到调试文件夹中。 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
SQL网络接口,错误:26 –指定服务器/实例时出错
in other pc. 在其他电脑上。
in the other hand when I change the cstring
like this 另一方面,当我像这样改变
cstring
Data Source=.\\SQLEXPRESS;AttachDbFileName=|DataDirectory|test.mdf;Integrated Security=true;
I have this error in my visual studio 我的视觉工作室有这个错误
Database '{0}' already exists.
数据库'{0}'已经存在。 Choose a different database name.
选择一个不同的数据库名称。 Cannot attach the file '{0}' as database '{1}'.
无法将文件“ {0}”作为数据库“ {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 ?? 是否有必要在另一台PC上使用服务器名称
SQLEXPRESS
和数据库名称test
?
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. 如果您希望应用程序以
*.mdf
文件连接到DB,则似乎在要处理该文件的PC上需要某种SQL Server安装。 Probably this can be either a local SQL server or a remote one. 可能是本地SQL Server或远程SQL Server。
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. 连接字符串的“
Data Source
部分中的“中”表示您引用运行该应用程序的本地计算机。 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. 因此,当您将应用程序移动到另一台PC上并将
Data Source
保留为.\\\\SQLEXPRESS
,您将引用该另一台PC上的SQLEXPRESS
SQL Server实例。 It probably doesn't have SQL Server installed (or has got another SQL instance name), which causes the error You described. 它可能没有安装SQL Server(或具有另一个SQL实例名称),这会导致您描述的错误。
So, basically, one way to fix the issue is by installing some form of SQL server on the other PC, if it's possible. 因此,基本上,解决此问题的一种方法是,如果可能的话,在另一台PC上安装某种形式的SQL Server。 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.
对于SQL Server 2012,可以使用特殊版本的SQL Server运行时,这些版本可以从Microsoft的站点单独下载。 For more information and additional explanation, refer to this answer on SO .
有关更多信息和其他说明,请参阅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. 如果您不想在本地安装SQL Server,也可以通过在另一台计算机上使用远程SQL Server来解决此问题。 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. 应替换为该SQL Server所在的计算机的名称,并且该计算机可以访问其他计算机。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.