【发布时间】:2019-07-13 10:42:31
【问题描述】:
我有执行基本 CRUD 操作的 C# 代码。我有一个定义为 AppDal 的类库,其中包含 Db 操作(用于 SQL Server),并且我在 C# 代码中使用命名空间。
如何使用相同的 c# 代码为另一个数据库(如 Oracle)执行数据库操作。我想在appsettings.json 中设置数据库名称。在此基础上,它将决定连接到哪个数据库。我将为另一个数据库(如 OracleDAL)创建另一个类库。
using AppDal;
public IActionResult Add(CustomerModel input)
{
ResponseModel response = new ResponseModel();
Dal dal = new Dal();
int Id = dal.Add(input);
if (Id > 0)
{
response.Id = Id;
response.Message = "Record added successfully";
}
else
{
response.Message = "Record not added";
}
return Json(response);
}
namespace AppDal
{
public int Add(CustomerModel input)
{
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("@Id",input.Id),
new SqlParameter("@CompanyId",input.CompanyId),
new SqlParameter("@Title",input.Title),
new SqlParameter("@Address",input.Address),
};
int result = SqlHelper.ExecuteScalar(SqlHelper.defaultDB, CommandType.Text, CustomerQueries.Add, param).ToInt();
return result;
}
}
如果我创建另一个应用程序是可能的,但这会增加维护时间。我想使用相同的应用程序。
【问题讨论】:
标签: c# asp.net asp.net-core asp.net-core-mvc