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值。