为什么Dapper样本中的#(哈希)

asg*_*las 7 c# dapper

我只是从Dapper"手册"中读到这个样本:

connection.Execute(@"
  set nocount on 
  create table #t(i int) 
  set nocount off 
  insert #t 
  select @a a union all select @b 
  set nocount on 
  drop table #t", new {a=1, b=2 })
   .IsEqualTo(2);
Run Code Online (Sandbox Code Playgroud)

#t是一种特殊的语法吗?或者他们只是在那里迷惑我?:)

Mar*_*ell 13

是的,这#意味着TSQL中有一些重要的东西 - 一个名为foopermenant 的表,用于该db/schema.名为的表#foo是临时表 - 它仅存在于该连接中,并在关闭或重置连接时被删除.名为的表##foo是一个全局临时表,并且存在于任何地方,但它是临时的.这主要用于批量移位数据.

#t这里的使用是为了使表只存在于该连接上,因此我们可以轻松地重新运行测试.

另外,一个指定的表@foo可以是一个表变量,或一个表值参数,并且只存在对命令/ SPROC.