取当前程序集的路径,然后加上“\database\access.mdb”就可以了,需要一点点反射技巧。
Type.Assembly.Location

解决方案 »

  1.   

    用相对路径
    Server.MapPath(@"database\access.mdb");
      

  2.   


    这样写不行说是找不到名称空间Server孟老这好像是WEB里的写法吧
      

  3.   

    public String ConnectAccess(String dbf,String psw)  // 产生Access连接串
    {
                   return("Provider=\"Microsoft.Jet.OLEDB.4.0\";"   +
                          "Jet OLEDB:Database Password="+psw+";"    +
           "Data Source=\""+dbf+"\";"                +
        "Password=;");                            
    }public String DbfPath(String dbf)  // 从当前目录往根目录找文件 dbf
    {
       String path=Application.StartupPath;
       bool root=false;   // 是否根目录
                   do 
       {
       String file1=path;
       root=(path[path.Length-1]=='\\');
                       if (!root)   
                           file1+="\\";
       file1+=dbf;  
       if (File.Exists(file1))
       return(file1);
       if (!root)
           path=Directory.GetParent(path).FullName;
       } while (!root);
                   return("");
    }private void Form1_Load(object sender, System.EventArgs e)
    {
                               String dbf0="dagl.mdb";
    String dbf1=DbfPath(dbf0);
    if (dbf1=="")
    {
    MessageBox.Show("文件: "+dbf0+" 不存在!");
    Application.Exit();
    return;
    }
    oleDbConnection1.ConnectionString=ConnectAccess(dbf1,"");
    oleDbConnection1.Open();
      

  4.   

    只要数据库的路径和应用程序的路径一样,或者数据库的路径与应用程序在同一根目录下。
    可以考虑用:
     [VB.NET]
     Dim currPath As String = System.Environment.CurrentDirectory
     Dim dbPath As String = currPath & "\" & DBNAME
     [C#]
     string currPath = System.Environment.CurrentDirectory;
     string dbPath = currPath + "\\" + DBNAME;
      

  5.   

    把数据库和你生成的exe放在一起!
    就是放在Debug目录下在连接字符串里直接写数据库名称不用写路径!:)
      

  6.   

    string DataAccessPath;
    DataAccessPath=Application.s.StartupPath;
      

  7.   

    string DataAccessPath;
    DataAccessPath=Application.StartupPath;
      

  8.   

    使用配制文件,写在Web.Config或app.Config中
      

  9.   

    winfrom里面没有Server.MapPath吧?