C# 在ASP.Net内核中实现ADO连接
我需要从.Net核心项目中的数据库中获取一个存储过程。通常,我通过执行以下操作来运行此存储过程: 首选代码C# 在ASP.Net内核中实现ADO连接,c#,C#,我需要从.Net核心项目中的数据库中获取一个存储过程。通常,我通过执行以下操作来运行此存储过程: 首选代码 readonly private SqlConnection _dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString); public int Insert(Employee employee) { var result =
readonly private SqlConnection _dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString);
public int Insert(Employee employee)
{
var result = 0;
using (var cmd = new SqlCommand("Sp_Insert", _dbConnection) { CommandType = CommandType.StoredProcedure })
{
try
{
cmd.Parameters.AddWithValue("@FirstName", employee.FirstName);
cmd.Parameters.AddWithValue("@LastName", employee.LastName);
cmd.Parameters.AddWithValue("@EmployeeCode", employee.EmployeeCode);
cmd.Parameters.AddWithValue("@Position", employee.Position);
cmd.Parameters.AddWithValue("@Office", employee.Office);
_dbConnection.Open();
result = cmd.ExecuteNonQuery();
}
catch
{
// ignore
}
finally
{
_dbConnection.Close();
}
}
return result;
}
{
"ConnectionStrings": {
"Default": "server=DESKTOP-98TG6JE\\SERVER_2014;database=vega;user=sa;password=ComplexPassword!123;"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
}
}
我的连接字符串在Web.config中
但对于.net核心,我的连接字符串位于appsettings.json中,如下所示:
.Net实体框架代码
readonly private SqlConnection _dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString);
public int Insert(Employee employee)
{
var result = 0;
using (var cmd = new SqlCommand("Sp_Insert", _dbConnection) { CommandType = CommandType.StoredProcedure })
{
try
{
cmd.Parameters.AddWithValue("@FirstName", employee.FirstName);
cmd.Parameters.AddWithValue("@LastName", employee.LastName);
cmd.Parameters.AddWithValue("@EmployeeCode", employee.EmployeeCode);
cmd.Parameters.AddWithValue("@Position", employee.Position);
cmd.Parameters.AddWithValue("@Office", employee.Office);
_dbConnection.Open();
result = cmd.ExecuteNonQuery();
}
catch
{
// ignore
}
finally
{
_dbConnection.Close();
}
}
return result;
}
{
"ConnectionStrings": {
"Default": "server=DESKTOP-98TG6JE\\SERVER_2014;database=vega;user=sa;password=ComplexPassword!123;"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
}
}
然后我创建一个DbContext,如下所示:
public class VegaDbContext : DbContext
{
public VegaDbContext(DbContextOptions<VegaDbContext> options) : base(options)
{}
public DbSet<Make> Makes { get; set; }
}
公共类VegaDbContext:DbContext
{
public VegaDbContext(DbContextOptions选项):基本(选项)
{}
公共DbSet使{get;set;}
}
然后在my Startup.cs中这样调用:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<VegaDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));
services.AddMvc();
}
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。使用此方法向容器中添加服务。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(options=>options.UseSqlServer(Configuration.GetConnectionString(“默认”));
services.AddMvc();
}
如果我使用实体框架进行CRUD,这是很好的,但是,我需要多次创建复杂的查询,因此我需要SQL存储过程。您能告诉我如何将我的“首选代码”与“.net实体框架代码”集成吗?多谢各位
请注意,如果可能的话,您能否以我上面的代码为例。进行以下更改:
- 在
方法中添加以下行:ConfigureServices
services.AddSingleton(配置)代码>
- 在使用
的InsertEmployee
中,添加类other
构造函数参数,调用它IConfiguration
,并将其设置为专用字段configuration
InsertEmployee
应该是什么样子:
public int InsertEmployee(Employee employee)
{
var sql = new SqlConnection(
this.configuration.GetConnectionString("Default"));
//... rest of your ADO code.
}
你看过这个吗?因此,我需要首先导入一些内容,但要在`new SqlConnection(ConfigurationManager.ConnectionStrings[“DbConnection”].ConnectionString);`,既然web.config中不再有“DbConnection”,我从哪里获得“DbConnection”呢?你看过这个了吗?我现在明白了。配置在Configure(…,…){var config=configuration[“DbConnection”]}中,但我的InsertEmployee在另一个类中。如何在新SqlConnection中使用它(ConfigurationManager.ConnectionString[“DbConnection”].ConnectionString)代码>应该通过DI提供:谢谢谢谢谢谢谢谢Thaaannnkkk yyooouuu!