在Dapper中创建临时表 ..net c#

huangapple go评论100阅读模式
英文:

Creating temporary table in Dapper ..net c#

问题

我试图使用Dapper创建一个临时表,但并不成功。我没有收到任何SQL异常或错误,连接已打开,但最终结果是"-1",没有创建临时表。有人知道我在哪里错了吗?

谢谢

英文:

I'm trying to create a #temporary table using Dapper without much success. I'm not receiving any SQL exceptions or errors, the connection is opened but the endresult is "-1" and no temporary table is created. Anyone know where I am going wrong here?

Thanks

  1. public abstract class DapperBaseRepository : IDisposable
  2. {
  3. private readonly IDbConnection _dbConnection;
  4. protected DapperBaseRepository()
  5. {
  6. _dbConnection = new SqlConnection(configuration.GetConnectionString("DefaultConnection"));
  7. }
  8. public async Task<int> CreateTempTable()
  9. {
  10. string mappingTable = "[dbo].[#IngredientSubstitutionUpload]";
  11. var query = @$"
  12. CREATE TABLE {mappingTable}(
  13. Row int NOT NULL,
  14. OriginalIngredient nvarchar(255) NOT NULL,
  15. OriginalSupplierCode nvarchar(255) NOT NULL,
  16. ReplacementIngredient nvarchar(255) NOT NULL,
  17. ReplacementSupplierCode nvarchar(255) NOT NULL)
  18. ";
  19. await _dbConnection.ExecuteAsync(query); // return -1
  20. // When attempting to insert here error is thrown: - Invalid object name '#IngredientSubstitutionUpload'.
  21. // Insert into temporary table
  22. await _dbConnection.ExecuteAsync(
  23. $"INSERT INTO {mappingTable} VALUES (@row, @originalIngredient, @originalSupplierCode, @replacementIngredient, @replacementSupplierCode)",
  24. substitutions.Select((x, idx) => new
  25. {
  26. row = idx,
  27. originalIngredient = x.OriginalIngredient,
  28. originalSupplierCode = x.OriginalSupplierCode,
  29. replacementIngredient = x.ReplacementIngredient,
  30. replacementSupplierCode = x.ReplacementSupplierCode
  31. }));
  32. }
  33. public void Dispose()
  34. {
  35. _dbConnection.Dispose();
  36. }
  37. }

答案1

得分: 2

你需要在调用ExecuteAsync语句之前明确打开连接。否则,Dapper将在每次ExecuteAsync之后自动关闭连接,使每次调用都具有单独的会话(临时表将无法存活)。

示例取自https://www.learndapper.com/misc/temporary-table:

  1. using (var connection = new SqlConnection(connectionString))
  2. {
  3. await conn.Open();
  4. connection.Execute("CREATE TABLE #tmpOrder(orderId int);");
  5. connection.Execute("INSERT INTO #tmpOrder(orderId) VALUES (1);");
  6. return connection.Query<int>("SELECT * FROM #tmpOrder;");
  7. }
英文:

You need to explicitly open the connection before calling the ExecuteAsyc statements. Otherwise Dapper will auto-close the connection after each ExecuteAsync giving each call a separate session (which the temp table will not survive).

Sample taken from https://www.learndapper.com/misc/temporary-table:

  1. using (var connection = new SqlConnection(connectionString))
  2. {
  3. await conn.Open();
  4. connection.Execute(@&quot;CREATE TABLE #tmpOrder(orderId int);&quot;);
  5. connection.Execute(@&quot;INSERT INTO #tmpOrder(orderId) VALUES (1);&quot;);
  6. return connection.Query&lt;int&gt;(@&quot;SELECT * FROM #tmpOrder;&quot;);
  7. }

huangapple
  • 本文由 发表于 2023年3月9日 23:46:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定