Using dapper, why is a temp table created in one use of a connection not available in a second use of the same connection

前端 未结 3 1761
有刺的猬
有刺的猬 2021-01-17 18:32

I\'m trying to perform a series of SQL*Server steps using dapper from C#. One step creates a temp table and populates it. Following steps query data from the temp table.

3条回答
  •  迷失自我
    2021-01-17 19:32

    The following works perfectly for me:

    db.Open();
    db.Execute(
        @"create table #foo (val int not null);
          insert #foo (val) values (123)");
    db.Execute(
        @"insert #foo (val) values (456)");
    var vals = db.Query(
        @"select * from #foo").ToList();
    foreach(var val in vals)
        Console.WriteLine(val);
    

    It also works perfectly if I use:

    @"select * from tempdb..#foo"
    

    The only way I can cause it to stop working is, as per my previous answer, to not open the connection first.

提交回复
热议问题