如何从mysql表中获取最新记录 [英] how get latest recordfrom mysql table

查看:118
本文介绍了如何从mysql表中获取最新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在开发访客跟踪系统,当访客来时,我们给了一张RFID卡,然后他将卡转入办公室,当他出来时,那张卡片交给了守望者。然后再把那张卡给了其他一些访客。



那么这里的问题是如何及时写出时间。以下是代码编写:


am developing visitor tracking system, when visitor come ,we r giving a RFID card,and he swips the card enters into office, when he come out, that card handovers to watchmen.then again that card given to some other visitor.

then here the problem is how to write in time and out time. Here is the code am writing:

String intime = "";
String outtime = "";

String indatetime=System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
String outdatetime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

using (MySqlConnection con = new MySqlConnection(ConnectionString))
{
   String query = "select * from visitor where card_id=@id ORDER BY card_id DESC LIMIT 1";
   MySqlCommand command = new MySqlCommand(query, con);
   command.Parameters.AddWithValue("@id", txtAddVisitorCardId.Text);
   con.Open();
   using (MySqlDataReader rdr = command.ExecuteReader())
   {
      if (rdr.Read())
      {
         intime = rdr["in_time"].ToString();
         outtime = rdr["out_time"].ToString();
      }
   }
}

if (((intime == "") && (outtime == "")) || ((intime!="")&&(outtime !="")))
{
   using (MySqlConnection con = new MySqlConnection(ConnectionString))
   {
      String query = "insert into visitor(card_id, name, age, address, id_proof, contact_person, purpose, in_time) values (@id, @name, @age, @address, @idproof, @contact, @purpose, @in)";
      MySqlCommand command = new MySqlCommand(query, con);
      command.Parameters.AddWithValue("@id", txtAddVisitorCardId.Text);
      command.Parameters.AddWithValue("@name", txtAddVisitorName.Text);
      command.Parameters.AddWithValue("@age", txtAddVisitorAge.Text);
      command.Parameters.AddWithValue("@address", txtAddVisitorAddress.Text);
      command.Parameters.AddWithValue("@idproof", txtAddVisitorIDProof.Text);
      command.Parameters.AddWithValue("@contact", txtAddVisitorContactPerson.Text);
      command.Parameters.AddWithValue("@purpose", txtAddVisitorPurpose.Text);
      command.Parameters.AddWithValue("@in", indatetime);
      con.Open();
      command.ExecuteNonQuery();
      MessageBox.Show("Visitor Added Successfully");
   }
}
else if ((intime != "") && (outtime == ""))
{
   using (MySqlConnection con = new MySqlConnection(ConnectionString))
   {
      String query = "update visitor set out_time=@out where card_id=@id ORDER BY card_id DESC LIMIT 1";
      MySqlCommand command = new MySqlCommand(query, con);
      command.Parameters.AddWithValue("@id", txtAddVisitorCardId.Text);
      command.Parameters.AddWithValue("@out", outdatetime);
      con.Open();
      command.ExecuteNonQuery();
   }
}

推荐答案

W&here4; Balboos [ ^ ]在他对这个问题的评论中给了你宝贵的指导。我想提供有关 AUTO_INCREMENT 的文章的链接[ ^ ]。在那里,您可以找到许多有关您的问题的有用信息。
W&here4; Balboos[^] gave you valuable instruction in His comment to the question. I'd like to provide a link to the article about AUTO_INCREMENT[^]. There you'll find many useful information about your issue.


这篇关于如何从mysql表中获取最新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆