public List<UsersModel> AccessDb()
        {
            List<UsersModel> list = new List<UsersModel>();
            string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(@"sample.mdb") + ";"; 
            OleDbConnection AConn=new OleDbConnection(ConnStr);
            AConn.Open();
            string  strSQL="SELECT   *   FROM   Users";
            OleDbCommand AComm = new OleDbCommand(strSQL, AConn);
            OleDbDataReader adr = AComm.ExecuteReader();
            while(adr.Read())
            {
                UsersModel usersModel = new UsersModel();
                usersModel.Uid = Convert.ToInt32(adr["Uid"]);
                usersModel.Uname = adr["Uname"].ToString();
                usersModel.Upwd = adr["Upwd"].ToString();
                list.Add(usersModel);
            }
            AConn.Close();
            return list;
        }包错找不到server
我想取数据库的相对路径但是总是不行

解决方案 »

  1.   

    跟踪一下看看
     把你的 Server.MapPath(@"sample.mdb") 声明成一个变量跟踪下看看是不是你要的路径
      

  2.   

    "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ HttpContext.Current.Server.MapPath("sample.mdb")
      

  3.   


    string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(@"sample.mdb") + ";"; sample.mdb放置的位置对吗?
      

  4.   

    错误 1 当前上下文中不存在名称“Server” D:\My Documents\GoShopChina\DAL\UsersDAL.cs 18 78 DAL
      

  5.   

     public static string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|sample.mdb;";我是把.mdb文件放在App_Code里面 然后写上面的话
      

  6.   

    string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
    string strDbPath = Server.MapPath("sample.mdb");
    再拼接在一起
      

  7.   

    用现成的吧,我常用的一个类
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.OleDb;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public class DataBase
        {
            private OleDbConnection getCon()
            {
                string strMdb = Application.StartupPath+"\\Data\\SoftWare.mdb";
                string strDataSource = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
                    + strMdb;
                OleDbConnection oledbCon = new OleDbConnection(strDataSource);
                return oledbCon;
            }
            OleDbConnection oledbcon;
            OleDbCommand oledbcom;
            OleDbDataAdapter oledbda;
            DataSet ds;        public void getCom(string strCon)
            {
                oledbcon = this.getCon();
                oledbcom = new OleDbCommand(strCon, oledbcon);
                oledbcon.Open();
                oledbcom.ExecuteNonQuery();
                oledbcon.Close();
            }        public DataSet getDs(string strCon, string tbname)
            {
                oledbcon = this.getCon();
                oledbda = new OleDbDataAdapter(strCon, oledbcon);
                ds = new DataSet();
                oledbda.Fill(ds, tbname);
                return ds;
            }        //public bool RsRepeat(string sql,string col)
            //{
            //    string temp = "t";
            //    DataSet ds=getDs(sql,temp);        //}
        }
    }
      

  8.   

    我记得我的blog里写过一篇access的文章,lz可以去看看