【问题标题】:Implement ADO connection in ASP.Net Core在 ASP.Net Core 中实现 ADO 连接
【发布时间】:2018-04-07 04:15:52
【问题描述】:

我需要从我的 .Net Core 项目中的数据库中获取一个存储过程。通常我通过这样做来运行这个存储过程:

首选代码

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

我的连接字符串在 Web.config 中 但是对于 .net Core,我的连接字符串位于 appsettings.json 中:

.Net 实体框架代码

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

然后像这样在我的 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();
}

如果我将实体框架用于 CRUD,这很好,但是,我需要多次创建复杂的查询,因此我需要 SQL 存储过程。您能否告诉我如何将我的“首选代码”与“.net 实体框架代码”集成?非常感谢。

附:如果可能的话,请您以我上面的代码为例。

【问题讨论】:

  • 你看过这个吗? stackoverflow.com/a/35444592/2638872
  • 所以我需要先导入一些东西,但是在`new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString);`上,我将在哪里得到“DbConnection”,因为在web.config 了?
  • 我现在明白了。配置在 Configure(..., ...) {var config = Configuration["DbConnection"]} 但我的 InsertEmployee 在另一个类中。我如何在new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString);中使用它
  • 它应该可以通过 DI 获得:stackoverflow.com/a/39232929/2638872

标签: c#


【解决方案1】:

进行以下更改:

  • 在您的 ConfigureServices 方法中添加以下行: services.AddSingleton&lt;IConfiguration&gt;(Configuration);

  • class otherInsertEmployee 中,添加 IConfiguration 构造函数参数,将其命名为 configuration 并将其设置为私有字段。

InsertEmployee 应该是这样的:

public int InsertEmployee(Employee employee)
{
    var sql = new SqlConnection(
        this.configuration.GetConnectionString("Default"));

   //... rest of your ADO code.
}

【讨论】:

  • 谢谢谢谢谢谢谢谢 thaaaannnkkk yyooouuu!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
相关资源
最近更新 更多