C# 数据库相对路径

请问用什么方法 可以这个数据的路径改成相对路径,自己建的一个data文件夹下面,这个文件夹在程序的根目录下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ID
{
public partial class Form1 : Form
{
OleDbConnection olecon;//声明OleDbConnection连接对象
OleDbDataAdapter oleda;//声明OleDbDataAdapter桥接器对象
DataSet myds;//声明DataSet数据集
//定义连接Access数据库的字符串
string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db_AutoID.mdb;";
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
olecon = new OleDbConnection(str);//实例化OleDbConnection连接对象
//查询数据表中所有数据
oleda = new OleDbDataAdapter("select * from tb_ID", olecon);
myds = new DataSet();//实例化DataSet数据集
oleda.Fill(myds);//填充DataSet数据集
dataGridView1.DataSource = myds.Tables[0];//为DataGridView控件设置数据源
//获取数据表中的最大编号
oleda = new OleDbDataAdapter("select max(ID) from tb_ID", olecon);
myds = new DataSet();//实例化DataSet数据集
oleda.Fill(myds);//填充DataSet数据集
if (myds.Tables[0].Rows.Count > 0)//判断DataSet数据集中是否有记录
{
//生成员工编号并显示
textBox1.Text = (Convert.ToInt32(myds.Tables[0].Rows[0][0]) + 1).ToString("0000");
}
else
{
textBox1.Text = "0000";//如果没有记录,初始化员工编号
}
}

private void button1_Click(object sender, EventArgs e)
{
olecon = new OleDbConnection(str);//实例化OleDbConnection连接对象
//实例化OleDbCommand对象
OleDbCommand olecmd = new OleDbCommand("insert into tb_ID values('" + textBox1.Text + "')", olecon);
olecon.Open();//打开数据库连接
olecmd.ExecuteNonQuery();//执行SQL语句
olecon.Close();//关闭数据库连接
Form1_Load(sender, e);//刷新窗体
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}
}
}

第1个回答  2011-05-24
很多种。Application.CommonAppDataPath是你的exe文件所在的路径,再加上你的文件夹名称就可以了。Application.CommonAppDataPath+"\FileFold"
第2个回答  2011-05-24
winform程序默认就是在当前运行的目录中,最后的exe文件在那,路径就在那,最好增加一个配置节,路径在什么地方,在配置文件中修改一下就行了
第3个回答  2011-05-24
Application.StartupPath就得到相对路径了,相对是的执行程序路径不包括文件名
第4个回答  2011-05-24
System.Enironment.CurrentDirectory或者Directory.GetCurrentDirectory()
第5个回答  2011-05-24
Server.MapPath(@"yourpath");本回答被提问者采纳