- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 C# 中使用 Dapper,后端是 MS Access。我的 DAL 方法在数据库中插入记录。我想返回插入行的唯一标识符(或更新的具有唯一标识符的 POCO)。我期待我的功能如下(我知道这不起作用;只是为了解释我想要什么):-
public MyPoco Insert(MyPoco myPoco)
{
sql = @"INSERT INTO MyTable (Field1, Field2) VALUES (@Field1, @Field2)";
var param = GetMappedParams(myPoco);//ID property here is null.
var result = _connection.Query<MyPoco>(sql, param, null, false, null, CommandType.Text);.Single();
return result;//This result now contains ID that is created by database.
}
我来自 NHibernate 世界,POCO 使用 NH 自动更新。如果不;我们可以调用 Refresh 方法并更新 ID。我不知道如何使用 Dapper 实现这一目标。
我读了this关于 SO 的问题与 SQL Server 无关。
另一个this问题没有可接受的答案。
我读了this接受的答案解释了使用 @@Identity
的问题。
最佳答案
这对我有用:
static MyPoco Insert(MyPoco myPoco)
{
string sql = "INSERT INTO MyTable (Field1, Field2) VALUES (@Field1, @Field2)";
_connection.Execute(sql, new {myPoco.Field1, myPoco.Field2});
myPoco.ID = _connection.Query<int>("SELECT @@IDENTITY").Single();
return myPoco; // This result now contains ID that is created by database.
}
请注意,这将与连接到 Access 数据库的 OleDbConnection
配合使用,但不能与 OdbcConnection
配合使用。
编辑回复:评论
为了确保连接在 INSERT 和 SELECT 调用之间保持打开状态,我们可以这样做:
static void Insert(MyPoco myPoco)
{
string sql = "INSERT INTO MyTable (Field1, Field2) VALUES (@Field1, @Field2)";
bool connAlreadyOpen = (_connection.State == System.Data.ConnectionState.Open);
if (!connAlreadyOpen)
{
_connection.Open();
}
_connection.Execute(sql, new {myPoco.Field1, myPoco.Field2});
myPoco.ID = _connection.Query<int>("SELECT @@IDENTITY").Single();
if (!connAlreadyOpen)
{
_connection.Close();
}
return; // (myPoco now contains ID that is created by database.)
}
关于c# - 小巧玲珑 + MSAccess : How to get identifier of inserted row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37391883/
下面使用Dapper抛出Incorrect syntax near ','。 const string sql = "select * from ZipToZipDistance z wher
我应该如何将 IDbConnection 和 IDbTransaction 与 Dapper 一起使用? 目前我只使用 IDbConnection 。如下: using (SqlConnection
这似乎是一种奇怪的行为,但以下代码会引发错误: public async Task RebuildIndex(string tableName, string indexName) { awa
在 MVC 3 项目中,我使用 EF4、IoC 和 Agatha-RRSL 作为我的服务层。 幸运的是,这周我找到了 Dapper,我将从 EF4 迁移到 Dapper! 通常我会在我的 Agatha
我今天设法启动并运行了一些小型沙箱/POC 项目,但似乎在一个问题上让我头疼...... 问题: Is there a way to get dapper to map to SQL column n
我的读者从存储过程中读取内容它的第一个选择语句之一是 select id , name from FSubsystem 从 10 个中选择 staments 然后我声明了如下字典Dictionary
我正在使用 dapper.net 并且我已经为我的 Delete 和 Update 语句包装了 connection.Execute。 public virtual void Update(TEnti
我的数据库是甲骨文。我使用 Dapper 1.13,它抛出一个异常说 Error parsing column 3 (LATITUDE=39.2330 - Decimal)。 LATITUDE 是我实
我正在使用 Dapper.NET,当我执行下一段代码时: using (SqlConnection con = new SqlConnection(connectionString))
我正在尝试使用结构作为 Dapper 中的参数查询。它对我不起作用。但好像it should work . 一个失败的例子: struct Simple { public int ID; } [Tes
我在 C# 中使用 Dapper,后端是 MS Access。我的 DAL 方法在数据库中插入记录。我想返回插入行的唯一标识符(或更新的具有唯一标识符的 POCO)。我期待我的功能如下(我知道这不起作
我看到了使用 Dapper 执行带有动态参数的存储过程并返回过程结果的示例。通常,示例使用 .Execute,但其中一些使用 .Query。我在使用 .Execute 时遇到困难。在上述情况下我应该使
实际上我有一个查询,它返回的结果包含 varchar 类型的列(对于 ex.Address),但是该表的域模型包含对象类型的属性(对于 ex.Address 地址)。因为它会出现错误,它说无法将字符串
我使用 Dapper 的 DynamicParamters 对象和模板参数来生成我的实体的参数。在调用我的存储过程后,出现以下错误“过程或函数 sp_MemberSave 指定的参数太多。”。我的一些
我有一个包含地址对象的公司。 SQL 返回是平坦的,我正在尝试让 Query<> 加载所有对象。 cnn.Query("Sproc", (org,mail,ph
我需要将项目集合传递给存储过程。存储过程将它们插入到多个表中。我使用声明的自定义用户类型的表值参数。 TVP 类型: CREATE TYPE [dbo].[Type1] AS TABLE (
我是一名优秀的程序员,十分优秀!