简体   繁体   English

OleDbConnection类别

[英]OleDbConnection Class

I created a class that execute OleDbConnection with a method and return this connection: 我创建了一个使用方法执行OleDbConnection的类并返回此连接:

public class ConnectDB
    {
        public static OleDbConnection getConStr() {

            return OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Microsoft.SqlServer.Server.MapPath("Users.accdb") + ";Persist Security Info=False");       
        }
    }

but I got an error in Server.MapPath ErrorMessage:The name 'Server' does not exist in the current context. 但我在Server.MapPath ErrorMessage中遇到错误:名称“服务器”在当前上下文中不存在。 How can I overcome this problem? 我该如何克服这个问题?

Use only Server.MapPath as follows: 仅使用Server.MapPath ,如下所示:

public class ConnectDB
    {
        public static OleDbConnection getConStr() {

            return OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="  +   Server.MapPath("Users.accdb") + ";Persist Security Info=False");       
        }
    }

Hope it helps. 希望能帮助到你。

you are probably using this code in a class library . 您可能正在class library使用此代码。

Server is an ASP.NET object, and you can access it inside an aspx page only or a Control-derived class. Server是一个ASP.NET对象,您只能在aspx页面或Control派生的类中访问它。

try this: 尝试这个:

 public class ConnectDB
 {
    public static OleDbConnection getConStr() {

        return OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="  +   HttpContext.Current.Server.MapPath("Users.accdb") + ";Persist Security Info=False");       
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM