2014-09-21 59 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); 

假设你已经在那里的文件,它应该工作。

相关问题