/// <summary> /// 更新 来访卡 状态 /// </summary> /// <param name="card_no"></param> /// <param name="flag"></param> /// <returns></returns> public static int updateFlag(string card_no, int flag) { //1.查询出数据。 //2. 符合条件则更新数据 int returnValue = 0; guest_card model = GetModel(card_no); if (model == null) { return(-1); } //并没有建立数据库连接 MySqlConnection conn = new MySqlConnection(connectStr); try { conn.Open(); //建立连接,打开数据库 card_no = card_no.Replace("'", "‘"); //防一下sql注入 string sqlstr = "update guest_card set card_flag ='" + flag + "' where card_no = '" + card_no + "' "; //SQL语句 MySqlCommand cmd = new MySqlCommand(sqlstr, conn); returnValue = cmd.ExecuteNonQuery(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { conn.Close(); //关闭连接 } return(returnValue); }
//获取 来访卡 信息 public static guest_card GetModel(string card_no) { guest_card model; //并没有建立数据库连接 MySqlConnection conn = new MySqlConnection(connectStr); try { conn.Open(); //建立连接,打开数据库 card_no = card_no.Replace("'", "‘"); //防一下sql注入 string sqlstr = "select * from guest_card where card_no = '" + card_no + "' "; //SQL语句 MySqlCommand cmd = new MySqlCommand(sqlstr, conn); //相当于数据读出流 理解为一本书 MySqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) //遍历表中数据,只取第一条记录 { //Console.WriteLine(reader.GetInt32("id") + reader.GetString("name") + reader.GetString("age")); //封装属性 model = new guest_card { //封装属性 card_id = reader.GetInt32("card_id"), card_no = reader.GetString("card_no"), card_flag = reader.GetInt32("card_flag") }; } else { model = null; } if (!reader.IsClosed) { reader.Close(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); return(null); } finally { conn.Close(); //关闭连接 } return(model); }