使用linq to sql创建数据库 [英] Creating database with linq to sql

查看:71
本文介绍了使用linq to sql创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法使用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创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆