2014-09-21 22 views
0

我特林連接到以w的WinForm C#應用程序 當我設置我的連接字符串數據的本地數據庫:Source=C:\Users\PACKARD BELL\documents\visual studio 2010\Projects\GestionStock\MyApp\Mydb.sdf
它工作正常,但是當我將其設置爲Data Source=|DataDirectory|\Mydb.sdf它這麼想的工作 我試圖在控制檯中的數據目錄變量打印連接字符串,我發現它這麼想的改變爲什麼在運行時不DataDirectory目錄變化

我要連接字符串與應用程序文件夾的位置改變

我該怎麼做? 由於事先

+0

不說,%數據目錄%或東西嗎? – Noctis 2014-09-21 21:52:22

+0

是的| DataDirectory |但它不會改變 – user3336078 2014-09-21 21:53:34

回答

0

根據MSDN

|DataDirectory|:這是通過AppDomain.SetData("DataDirectory", objValue)方法設置的值。 ASP.NET應用程序解析| DataDirectory |到「/ app_data」文件夾。

事實上,如果你使用|DataDirectory|值,數據庫必須存在於您的項目App_Data\目錄內。您可以更改默認值,但實際上這隻會影響其他可能期望默認行爲的開發人員。

所以,相反的

Source=C:\Users\PACKARD BELL\documents\visual studio 2010/Projects\GestionStock\MyApp\Mydb.sdf

這將是

Source=C:\Users\PACKARD BELL\documents\visual studio 2010\Projects\GestionStock\MyApp\App_Data\Mydb.sdf

+0

我確實把我的sdf文件放在App_Data目錄中仍然不起作用 – user3336078 2014-09-21 22:09:34

+0

對不起,它工作正常我拼錯了 – user3336078 2014-09-21 22:12:33

0

但該文件夾的App_Data

+0

我該如何使用它? – user3336078 2014-09-21 21:57:07

+0

只需在項目中創建一個名爲App_data的文件夾,或者在解決方案資源管理器中右鍵單擊項目時,您可以從空間文件夾菜單中進行選擇。 – 2014-09-21 21:59:04

0

在這裏,在SDF文件是從article廢料我寫道:

// What's the name of the file you want to work with 
var file_name = "parse_me.txt"; 

// Let's assume you're working with the file on your desktop 
var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 

// This is needed to "paste" the dir and the file name (it'll add the "\" basically). 
var file_location = Path.Combine(path,file_name); 

// Now you can do: 
string[] read_all_lines = System.IO.File.ReadAllLines(file_location); 

假設你已經在那裏的文件,它應該工作。

相關問題