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;

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章