简体   繁体   English

没有实体框架数据库连接的ASP.NET MVC

[英]ASP.NET MVC without Entity Framework database connection

I have a project that uses ASP.NET MVC and follows three tier architecture. 我有一个使用ASP.NET MVC并遵循三层体系结构的项目。 I want to add a class library that will have a base class which will contain the connection string and in the child class that connection string has to be called by pass the data source as string. 我想添加一个类库,该类库将具有一个包含连接字符串的基类,在子类中,必须通过将数据源作为字符串传递来调用连接字符串。

Base class: 基类:

public class DALConnection {
     public SqlConnection con = new SqlConnection("Data Source=ABC;Initial Catalog=HotelMgmt;Integrated Security=True");
}

Child class: 子班:

public class DataAccessOperation: DALConnection {
    // Some code
}

Instead of this I want to pass the connection string to the base class through child class as string. 取而代之的是,我想通过子类将连接字符串作为字符串传递给基类。

Do you think you could use the constructors to do that? 您认为可以使用构造函数来做到这一点吗? (as below) (如下)

Base class: 基类:

public class DALConnection {

     public SqlConnection con;

     public DALConnection(string connectionString)
     {
          con = new SqlConnection(connectionString);
     }

}

Child class: 子班:

public class DataAccessOperation: DALConnection {

    //Some code
    public DataAccessOperation() : base("Data Source=ABC;Initial Catalog=HotelMgmt;Integrated Security=True")
    {

    }
}

Use a constructor to pass the connections strings and have the derived class call the base constructor. 使用构造函数传递连接字符串,并使派生类调用基本构造函数。

Base class 基类

public class DALConnection {
    protected string connectionString = "Data Source=ABC;Initial Catalog=HotelMgmt;Integrated Security=True";
    public SqlConnection con;

    public DALConnection(string connectionString) {
        if(connectionString!=null)
            this.connectionString = connectionString;
        con = new SqlConnection(this.connectionString);
    }
}

Child class: 子班:

public class DataAccessOperation: DALConnection {

    DataAccessOperation(string connectionString) : base(connectionString) {
    }

}

public class DALConnection 
{
      public DALConnection(string _conString)
      {
            if(!String.IsNullOrEmpty(_conString))
            {
                con=new SqlConnection(_conString);
            }
            else 
            {
                //error handling
            }
      }      

      private SqlConnection con;

}

public class DataAccessOperation: DALConnection 
{
      public DataAccessOperation (string _conString) :base (_conString)
      {
      }


}

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

相关问题 ASP.NET MVC2和Entity Framework 4从数据库生成模型,而无需复制成员资格模型 - ASP.NET MVC2 and Entity Framework 4 Generating Model from Database without duplicating Membership Models 努力使用实体框架将数据保存到ASP.NET MVC中的数据库 - Struggling with saving data to database in ASP.NET MVC with Entity Framework PopUp 不在 ASP.NET MVC 5 和实体框架中的数据库中发送数据 - PopUp not sending data in database in ASP.NET MVC 5 and Entity Framework ASP.net MVC与Entity Framework重新排序数据库记录 - ASP.net MVC with Entity Framework reordering database records ASP.NET MVC 4实体框架数据库第一个存储库 - ASP.NET MVC 4 Entity Framework Database First Repository 使用 asp.net mvc 4 + Entity Framework 将图像保存到数据库 - Saving images to database with asp.net mvc 4 + Entity Framework 配方-Entity Framework ASP.NET MVC中的成分数据库 - Recipe - ingredients database in Entity Framework ASP.NET MVC Entity Framework Core 和 ASP.NET Core MVC 中的 Inheritance 数据库 - Inheritance Database in Entity Framework Core and ASP.NET Core MVC ASP.NET 5 MVC 6中没有实体框架的身份验证和授权 - Authentication and Authorization without Entity Framework in ASP.NET 5 MVC 6 ASP.NET MVC 5应用程序-没有实体框架的存储库模式 - ASP.NET MVC 5 application - Repository pattern without Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM