第一个问题:
说我有
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string storedProc = "GetData";
SqlCommand command = new SqlCommand(storedProc, connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@EmployeeID", employeeID));
return (byte[])command.ExecuteScalar();
}
Run Code Online (Sandbox Code Playgroud)
连接是否关闭?因为从技术上讲,我们永远不会}像return以前那样到达最后.
第二个问题:
这次我有:
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
int employeeID = findEmployeeID();
connection.Open();
SqlCommand command = new SqlCommand("UpdateEmployeeTable", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@EmployeeID", employeeID));
command.CommandTimeout = 5;
command.ExecuteNonQuery();
}
}
catch (Exception) { /*Handle error*/ }
Run Code Online (Sandbox Code Playgroud)
现在,说出来try我们得到一个错误,它被抓住了.连接是否仍然关闭?因为我们再次跳过其余代码try并直接转到catch …
我想在多个表上运行多个insert语句.我正在使用dapper.net.我没有看到任何方法来处理与dapper.net的交易.
请分享您对如何使用dapper.net进行交易的想法.
目前,我正在尝试使用Dapper ORM与工作单元+存储库模式.
我想使用工作单元而不是简单的dapper存储库,因为我的插入和更新需要一定程度的事务处理.我一直无法找到任何有用的例子,因为大多数似乎使用实体框架并且在工作单元内存在泄漏问题.
有人可以指点我正确的方向吗?
我正在寻找在.net Core 2 MVC应用程序中的appsettings.json中存储连接字符串的最佳实践方法(就像你在MVC 5中的web.config中所做的那样).
我想使用Dapper而不是EF(我发现了许多EF示例).
像这样的东西:
{
"ConnectionStrings": {
"myDatabase": "Server=.;Database=myDatabase;Trusted_Connection=true;"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
Run Code Online (Sandbox Code Playgroud)
当然网上有很多例子吗?我无法找到.net core 2.0.
在1和2之间有一些变化,我想确保我使用的是第2版最佳实践.
我发现了这个 - 但似乎是.net核心1: Visual Studio 2017 - MVC核心 - 第05部分 - 来自appsettings.json的连接字符串
这使用键值对appsettings - 而不是连接字符串: 在ASP.NET Core 2.0中读取AppSettings
再次,目前还不清楚这是.net Core 1还是2:Net Core Connection String Dapper visual studio 2017
我正在努力使用, &NpgsqlConnection()中的多个连接字符串注册 DI 。ASP.NET Core 3.1Dapper v2.0.78Postgres v11
我将提供当前状态并修复以下代码:
步骤1。Startup.cs --> ConfigureServices()
services.AddTransient<IDbConnectionFactory, DapperDbConnectionFactory>(sp =>
{
var connectionDict = new Dictionary<DatabaseConnectionName, string>
{
{ DatabaseConnectionName.Cnn1, "Connectionstring 1"},
{ DatabaseConnectionName.Cnn2, "Connectionstring 2"}
};
return new DapperDbConnectionFactory(connectionDict);
});
Run Code Online (Sandbox Code Playgroud)
第2步。DapperDbConnectionFactory看起来像这样:
public class DapperDbConnectionFactory : IDbConnectionFactory
{
private readonly IDictionary<DatabaseConnectionName, string> _connectionDict;
public DapperDbConnectionFactory(IDictionary<DatabaseConnectionName, string> connectionDict)
{
_connectionDict = connectionDict;
}
public IDbConnection CreateDbConnection(DatabaseConnectionName connectionName)
{
string connectionString = null;
if …Run Code Online (Sandbox Code Playgroud) c# postgresql dependency-injection connection-pooling asp.net-core
我在我的 asp.net 核心 Web API 项目中使用 Dapper ORM 进行数据库操作。现在我每次都打开新的数据库连接并在using块中使用它,以便在范围结束时将它们处理掉。但是我希望在不使用using块的情况下处理所有这些连接,并且还想自动处理它们。我正在寻找一种方法来使用依赖注入来实现这一点,因为它们会自动处理实现 IDisposable 的对象。
这是我处理所有数据库连接的方式:
在我的基础存储库中创建了一个 GetConnection 属性:
private IDbConnection _connection;
public IDbConnection GetConnection
{
get
{
_connection = new NpgsqlConnection("Connection String");
return _connection;
}
}
Run Code Online (Sandbox Code Playgroud)
使用块访问内部的属性:
public async Task<IEnumerable<T>> GetAllAsync()
{
IEnumerable<T> records = null;
using (IDbConnection connection = GetConnection)
{
//db operations
}
return records;
}
Run Code Online (Sandbox Code Playgroud)
那么如何使用依赖注入实现相同的功能,在需要时初始化 IDbconnection 并在请求结束时处理,而无需将 IDbconnection 封装在 using 块中?
简而言之,我想避免每次都使用 GetConnection 属性来创建数据库对象,并避免使用块来处理相同的对象。
我见过有人在做的例子:
IDbConnection db = new MySqlConnection(conn);
var people = db.Query<People>("SELECT * FROM PEOPLE").ToList();
Run Code Online (Sandbox Code Playgroud)
或者上面是一个不好的做法,并且所有查询都应该使用如下语句:
using (var db = new MySqlConnection(conn))
{
var people = db.Query<People>("SELECT * FROM PEOPLE").ToList();
}
Run Code Online (Sandbox Code Playgroud) 我正在使用 Dapper 和 Dapper.Contrib 从数据库映射对象。
我有一个类名,我在其中定义该类的表名,因为它与实际的类名不同。
班级:
[Table("tblUser")]
public class User
{
public int Id { get; set; }
public string Title { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如何获取设置表数据注释属性的表名?
编辑:
我用以下方法让它工作
var tAttribute =
(TableAttribute)typeof(T).GetCustomAttributes(typeof(TableAttribute), true)[0];
tableName = tAttribute.Name;
Run Code Online (Sandbox Code Playgroud) dapper ×5
c# ×4
asp.net-core ×3
micro-orm ×1
npgsql ×1
postgresql ×1
transactions ×1
typeof ×1
unit-of-work ×1
using ×1