所以我使用 Dapper 进行了一个简单的更新:
using (var conn = new SqlConnection(myConnectionString))
{
conn.Open();
conn.Execute("UPDATE Orders SET Exported=1 WHERE ManufacturerID=@ManufactuterID AND OrderID=@OrderID",
new { ManufacturerID = ManufacturerID, OrderID = OrderID });
conn.Close();
}
Run Code Online (Sandbox Code Playgroud)
我怎么知道这确实有效?我没有看到使用 Dapper 运行 ExecuteNonQuery 的方法?
在使用 Dapper.TVP TableValueParameter 与其他参数之后, 我已经能够执行表值参数,但我的问题是关于其实现的一部分。程序似乎有效,但我怎样才能使这个实现更加通用?根据需求在 DynamicParameters 中使用“new {}”添加参数是非常专业的实现。我也希望能够指定我的参数名称..例如
public IEnumerable<TCustomEntity> SqlQuery<TCustomEntity>(string query, IDictionary<string, object> parameters,
CommandType commandType) where TCustomEntity : class
{
DataTable dt;
DynamicParameters dp;
var sqlConnection = _context.Database.Connection;
try
{
if (sqlConnection.State == ConnectionState.Closed)
{
sqlConnection.Open();
}
var dynamicParameters = new DynamicParameters();
if (parameters != null)
foreach (var parameter in parameters)
{
if (parameter.Value is int)
dynamicParameters.Add(parameter.Key, parameter.Value, DbType.Int32);
else if (parameter.Value is string)
dynamicParameters.Add(parameter.Key, parameter.Value.ToString(), DbType.String);
else if (parameter.Value is DateTime)
dynamicParameters.Add(parameter.Key, parameter.Value, DbType.DateTime);
else if …Run Code Online (Sandbox Code Playgroud) 我需要导入多个sql server关系表中的数百万条记录。
TableA(Aid(pk),Name,Color)----return id using scope identity
TableB(Bid,Aid(fk),Name)---Here we need to insert Aid(pk) which we got using scocpe Identity
Run Code Online (Sandbox Code Playgroud)
如何在一个 Insert 语句中使用 dapper 批量插入数百万条记录的集合
尝试使用SqlDataReaderdapper 但在标题上出现错误。下面是代码:
using (var reader = (SqlDataReader)con.ExecuteReader(query))
{
while (reader.Read())
{
//do stuff here with reader
}
}
Run Code Online (Sandbox Code Playgroud) 我使用“Dapper”库与 MS Sql 连接。
当我调用存储过程时,它显示错误消息
找不到存储过程。
但存储过程它已经存在于数据库中。我该如何解决这个问题?谢谢。
public static string WrtoLogDb(string id, string id1)
{
using (var conn = new SqlConnection(connStrTest))
{
try
{
conn.Open();
var strf33_schD = conn.ExecuteScalar<int>("exec DelClsCourTimeTest", new { id, id1 }, commandType: CommandType.StoredProcedure);
return strf33_schD.tostring();
}
catch (Exception EX)
{
return EX.ToString();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有许多从数据库加载的记录,我想在返回它们之前更新设置的值。唯一的主要要求是我不希望多个命令一一更新每条记录。
请注意我的 ID 是 GUID(唯一标识符)
到目前为止我的代码是
public IEnumerable<Person> GetUnprocessedPeople(int batchSize)
{
List<Queue_ImportQueue> list;
using (IDbConnection db = OpenedConnection)
{
string peopleList = $"SELECT TOP({batchSize}) * FROM [dbo].[Person]";
list = db.Query<Person>(peopleList).ToList();
using (IDbTransaction transactionScope = db.BeginTransaction(IsolationLevel.Serializable))
{
string updateQuery = $"UPDATE [dbo].[Person] SET Created = GETDATE() WHERE Id ='@ids'";
try
{
db.Execute(updateQuery, new { Id = list.Select(x => x.Id) }, transactionScope);
transactionScope.Commit();
}
catch (Exception ex)
{
transactionScope.Rollback();
throw;
}
}
}
return list;
}
Run Code Online (Sandbox Code Playgroud) string query = $@"SELECT m.* FROM Table1";
return await dbConnection.QueryAsync<Job>();
Run Code Online (Sandbox Code Playgroud)
在 Oracle 数据库表中Table1包含许多列,我不想指定所有列。列在 中返回UPPERCASE,但在 .NET 中我们使用 CamelCase(IsActive等等)。结果,大写列永远不会在代码中映射到 CamelCase。(实际上查询更复杂,还有一些自定义映射和 splitOn 选项)。
可以用Dapper解决吗?
我使用 Dapper (使用npgsql带有插件的数据提供程序NetTopologySuite)来调用带有参数的 PostgreSQL 函数geography,然后收到一个NotSupportedException:
System.NotSupportedException: The member _location of type NetTopologySuite.Geometries.Point cannot be used as a parameter value
at Dapper.SqlMapper.LookupDbType(Type type, String name, Boolean demand, ITypeHandler& handler) in C:\projects\dapper\Dapper\SqlMapper.cs:line 417
at Dapper.SqlMapper.CreateParamInfoGenerator(Identity identity, Boolean checkForDuplicates, Boolean removeUnused, IList`1 literals) in C:\projects\dapper\Dapper\SqlMapper.cs:line 2516
at Dapper.SqlMapper.GetCacheInfo(Identity identity, Object exampleParameters, Boolean addToCache) in C:\projects\dapper\Dapper\SqlMapper.cs:line 1707
at Dapper.SqlMapper.ExecuteScalarImplAsync[T](IDbConnection cnn, CommandDefinition command) in C:\projects\dapper\Dapper\SqlMapper.Async.cs:line 1207
...
Run Code Online (Sandbox Code Playgroud)
但当我使用NpgsqlCommand通过方法指定的类型时,它工作得很好AddWithValue。
我怎样才能将 Dapper 映射NetTopologySuite.Geometries.Point到 …
我的代码看起来像这样;
using (IDbConnection cnn = new SqlConnection(GetConnectionString()))
{
var result = cnn.QuerySingle(sql, parameters);
return result;
}
Run Code Online (Sandbox Code Playgroud)
它在类内的方法内,但这无关紧要。据说 QuerySingle 返回一个动态类型,但我以前从未使用过它。当我调试这段代码时,它返回的内容是这样的;
{{DapperRow, Tcode = 'eeeee', Hashedpw = 'NF886jMDl5imyMj0ThDIxA==', Salt = 'Z+HHq6Rt60FPnAf80Lg4Dg=='}}
Run Code Online (Sandbox Code Playgroud)
我该如何阅读?它们是键值对是因为背后的逻辑不起作用,它不像列表或数组那样工作。我完全迷失了,不知道这会返回什么。我只需要这些值,而不是 dapperrow、tcode、hashedpw 和 salt。所以基本上,我需要的回报是
['eeeee', 'NF886jMDl5imyMj0ThDIxA==','Z+HHq6Rt60FPnAf80Lg4Dg==']
Run Code Online (Sandbox Code Playgroud)
谁能帮我吗?
我TotalPrice在我们的 SQL Server 2016 数据库中有一个numeric(20, 3)数据类型的表字段()。
我使用ulongtype 作为我的 C# 属性类型。
public ulong TotalPrice { get; set; }
Run Code Online (Sandbox Code Playgroud)
当我想在我的表中插入一条记录时,发生了以下异常。
不存在从 DbType UInt64 到已知 SqlDbType 的映射
我的 C# 代码插入记录:
const string queryInsertInvoice = @"
INSERT INTO Invoice (CustomerId, Number, TotalPrice, LatestStatusDateTime)
VALUES (@CustomerId, @Number, @TotalPrice, @LatestStatusDateTime)
SELECT SCOPE_IDENTITY();";
var invoiceId = await dbConnection.QueryFirstOrDefaultAsync<int>(queryInsertInvoice, invoice);
Run Code Online (Sandbox Code Playgroud)
我如何使用 Dapper 2.0.53 处理这种情况?
dapper ×10
c# ×8
sql-server ×3
asp.net-mvc ×2
.net ×1
asp.net ×1
c#-4.0 ×1
npgsql ×1
postgresql ×1
sql ×1