欢迎来到.net学习网

欢迎联系站长一起更新本网站!QQ:879621940

您当前所在位置:首页 » Sql随手笔记 » 正文

热门阅读

SELECT @@IDENTITY的意思

创建时间:2011年06月09日 20:31  阅读次数:(5083)
分享到:

public int Append ( messageInfo info )
  {
   int newid = 0;
   string Sql = "INSERT INTO [message] ([mes_type],[mes_title],[mes_name],[mes_type1],[mes_type2],[mes_type3],[mes_pingbai],[mes_xinghao],[mes_fanwei],[mes_tixi],[mes_suxin],[mes_content],[mes_pic],[mes_time],[mes_danwei],[mes_danjia],[mes_liang],[mes_total],[mes_time2],[user_id],[mes_data],[state],[refusal]) VALUES (@type,@title,@name,@type1,@type2,@type3,@pingbai,@xinghao,@fanwei,@tixi,@suxin,@content,@pic,@time,@danwei,@danjia,@liang,@total,@time2,@id,@data,@state,@refusal); SELECT @@IDENTITY;";
   ServiceParameter[] parameters = new ServiceParameter[]{
    new ServiceParameter("@type", info.type),
    new ServiceParameter("@title", info.title),
    new ServiceParameter("@name", info.name),
    new ServiceParameter("@type1", info.type1),
    new ServiceParameter("@type2", info.type2),
    new ServiceParameter("@type3", info.type3),
    new ServiceParameter("@pingbai", info.pingbai),
    new ServiceParameter("@xinghao", info.xinghao),
    new ServiceParameter("@fanwei", info.fanwei),
    new ServiceParameter("@tixi", info.tixi),
    new ServiceParameter("@suxin", info.suxin),
    new ServiceParameter("@content", info.content),
    new ServiceParameter("@pic", info.pic),
    new ServiceParameter("@time", info.time),
    new ServiceParameter("@danwei", info.danwei),
    new ServiceParameter("@danjia", info.danjia),
    new ServiceParameter("@liang", info.liang),
    new ServiceParameter("@total", info.total),
    new ServiceParameter("@time2", info.time2),
    new ServiceParameter("@id", info.id),
    new ServiceParameter("@data", info.data),
    new ServiceParameter("@state", info.state),
    new ServiceParameter("@refusal", info.refusal)
   };
            using ( IDataReader dr = ServiceProvider.GetProvider().GetReader(Sql, CommandType.Text, parameters) )
            {
                if ( dr.Read() ) int.TryParse(dr.GetDecimal(0).ToString(), out newid);
                dr.Close();
            }
            return newid;
  }


@@identity是表示的是最近一次向具有identity属性(即自增列)的表插入数据时对应的自增列的值,是系统定义的全局变量。一般系统定义的全局变量都是以@@开头,用户自定义变量以@开头。比如有个表A,它的自增列是id,当向A表插入一行数据后,如果插入数据后自增列的值自动增加至101,则通过select @@identity得到的值就是101。使用@@identity的前提是在进行insert操作后,执行select @@identity的时候连接没有关闭,否则得到的将是NULL值。

来源:
说明:所有来源为 .net学习网的文章均为原创,如有转载,请在转载处标注本页地址,谢谢!
【编辑:Wyf

打赏

取消

感谢您的支持,我会做的更好!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

最新评论

共有评论0条
  • 暂无任何评论,请留下您对本文章的看法,共同参入讨论!
发表评论:
留言人:
内  容:
请输入问题 73+59=? 的结果(结果是:132)
结  果: