简体   繁体   English

使用连接字符串连接数据库时出错,并且附加数据库文件时出错

[英]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.

相关问题 使用 Hangfire,Startup.cs 中给出的连接字符串抛出无法附加文件作为数据库错误 - Using Hangfire, connection string given in Startup.cs throws Cannot attach file as database error 附加数据库连接字符串 - Attach database connection string 连接字符串不正确? 错误:尝试附加自动命名的数据库失败 - Incorrect connection string? Error: An attempt to attach an auto-named database failed C# Dapper SQL 服务器连接错误:连接字符串中不存在 Dapper 连接到另一个数据库 - C# Dapper SQL Server Connection Error : Dapper Connect to anther database not exist in connection string 为什么将Oracle DB与C#连接时出现连接字符串错误? - Why i get the connection string error when connect the oracle DB with c#? SQL 服务器连接字符串错误无法连接 - SQL Server connection string error cannot connect 每当我连接到SQL Server数据库时,是否都必须使用附加连接字符串? - Do I have to use attach connection string whenever I connect to a SQL Server database? 尽管连接字符串正确,但无法连接到服务器上的 Azure 数据库。 1042 错误 - Can't connect to Azure database on Sever although the connection String is right. 1042 error 插入数据库C#连接字符串错误 - Insert into database c# connection string error C#中的连接字符串错误数据库 - connection String Error database in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM