将DB附加到SQL服务器:string.format问题 [英] Attach DB to SQL server: string.format problem
问题描述
您好,
我正在尝试将exisitng数据库附加到SQL Server 2008.
当我使用时:
Hello,
I am trying to attach exisitng database to SQL Server 2008.
When I use:
str = String.Format("EXEC sp_attach_db @dbname = '{0}',@filename1 = '{1}',@filename2 = '{2}'", databaseName, mdfFile, ldfFile);
com = new SqlCommand(str, myConn);
我收到错误:无法打开物理文件C:\ Testst \ MyProgram_Data。中密度纤维板。操作系统错误2:2(未找到错误)。
我尝试过:
但是当我使用时:
I get an error: Unable to open the physical file "C:\TestDB\MyProgram_Data.mdf". Operating system error 2: "2(error not found)".
What I have tried:
But when I use:
str = "EXEC sp_attach_db @dbname = N'MyProgram' ,@filename1 = N'C:\\TestDB\\MyProgram\\MyProgram_Data.mdf',@filename2 = N'C:\\TestDB\\MyProgram\\MyProgram_Log.ldf'";
一切都好。
为什么我无法使用string.Format()?
Everything is OK.
Why I am not able to use string.Format()?
推荐答案
所以这个工作
So this works
C:\TestDB\MyProgram\MyProgram_Data.mdf
......这不是
...and this doesn't
C:\TestDB\MyProgram_Data.mdf
找出差价?
mdfFile和ldfFile没有与数据库所在位置匹配的正确值。
Spot the difference?
"mdfFile" and "ldfFile" do not have the correct values that match where your database is.
因为路径错误:
Because the path is wrong:
Unable to open the physical file "C:\TestDB\MyProgram_Data.mdf".
不起作用,但是:
Doesn't work, but:
str = "EXEC sp_attach_db @dbname = N'MyProgram' ,@filename1 = N'C:\\TestDB\\MyProgram\\MyProgram_Data.mdf'
。它有一个额外的文件夹名称:MyProgram
Does. It has an extra folder name in there: "MyProgram"
这篇关于将DB附加到SQL服务器:string.format问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!