C#连接数据库时的相对路径
[global::System.Configuration.DefaultSettingValueAttribute("Provider=Microsoft.Jet.OL...
[global::System.Configuration.DefaultSettingValueAttribute("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\新建文件夹\\students.mdb")]//绝对路径
展开
1个回答
展开全部
1、配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="connString" connectionString="Data Source=|path|\db\database.s3db;Version=3;" providerName="System.Data.SQLite" />
</connectionStrings>
</configuration>
2、实例化时传参,Application.StartupPath返回当前程序运行路径。
DBHelper db = new DBHelper(Application.StartupPath);
3、构造方法
public DBHelper(string dbPath)
{
this.connString = this.connString.Replace("|path|", dbPath);
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="connString" connectionString="Data Source=|path|\db\database.s3db;Version=3;" providerName="System.Data.SQLite" />
</connectionStrings>
</configuration>
2、实例化时传参,Application.StartupPath返回当前程序运行路径。
DBHelper db = new DBHelper(Application.StartupPath);
3、构造方法
public DBHelper(string dbPath)
{
this.connString = this.connString.Replace("|path|", dbPath);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询