C#获得当前插入数据的ID

int flag=0;
   Connection.Open();
   string sql="insert into MsgInfo(title,content,sendtime) values('"+bVO.Subject+"','"+bVO.Content+"','"+bVO.Fbtime+"')";
   SqlCommand sqlComm = new SqlCommand(sql,Connection);
   flag = sqlComm.ExecuteNonQuery();
   sql="select @@identity as id from MsgInfo";
   sqlComm = new SqlCommand(sql,Connection);
   SqlDataReader tmpReader = sqlComm.ExecuteReader();
   if(tmpReader.Read())
   {
    flag = Convert.ToInt32(tmpReader["id"].ToString());
  
}
   Connection.Close();
   return flag;

posted @ 2007-12-27 15:47  生活无限  阅读(1083)  评论(4编辑  收藏  举报