问题描述
我设法使用linq to sql通过以下代码创建了一个数据库:
I managed to create a database using linq to sql with the following code:
private static DataClasses1DataContext _dataDC = new DataClasses1DataContext(@"C:\\database1.mdf");
public AddClient()
{
InitializeComponent();
DataContext = this;
_dataDC.CreateDatabase();
}
这样就在C:上创建了一个新的本地数据库.
如何更改@"C:\\ database1.mdf",以便在运行程序的当前目录中创建文件?
and this created a new local database on C: .
How can i change the @"C:\\database1.mdf" so that the file is created in the current directory from where i run the program ?
推荐答案
string path = Application.ExecutablePath;
string betterPath = Application.CommonAppDataPath;
第一个获取可执行应用程序EXE文件-因此您可以通过以下方式使用该文件夹:
The first gets the executable application EXE file - so you can use the folder by:
string justPath = Path.GetDirectoryName(path);
但是使用DataPath是一个更好的主意-在发行版本中写入EXE路径可能会遇到麻烦,因为它位于"Program Files"文件夹下,并且需要管理员访问权限.
But it is a much, much better idea to use the DataPath - you may well havce problems writing to the EXE path in release versions as it will be below the "Program Files" folder and require Admin access.
这篇关于使用linq to sql创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!