简体   繁体   中英

OleDbConnection Class

I created a class that execute OleDbConnection with a method and return this connection:

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. How can I overcome this problem?

Use only Server.MapPath as follows:

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 .

Server is an ASP.NET object, and you can access it inside an aspx page only or a Control-derived class.

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");       
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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