.NET MySQL的参数化查询

MySqlConnection conn = new MySqlConnection(SqlConnnectString);

MySqlCommand cmd = new MySqlCommand("SELECT * FROM `TB_CM_Log` WHERE ID = ?ID LIMIT 1", conn);

cmd.Parameters.AddWithValue("ID", id);

cmd.CommandType = CommandType.Text;

cmd.Parameters.Clear();

TB_CM_Log model = new TB_CM_Log();

if (conn.State != ConnectionState.Open)
     conn.Open();

using (MySqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
{
      while (dr.Read())
      {
             model.ID = dr.GetString(0);
             model.ModType = dr.GetInt32(1);
             model.ModTypeName = dr.GetString(2);
             model.SourcesType = dr.GetString(3);
             model.Description = dr.GetString(4);
             model.OperatorID = dr.GetInt32(5);
             model.OperatorName = dr.GetString(6);
             model.SucFlag = dr.GetInt32(7);
             model.OperaTime = dr.GetDateTime(8);
             model.IP = dr.IsDBNull(9) ? "" : dr.GetString(9);
             model.ResultDes = dr.IsDBNull(10) ? "" : dr.GetString(10);
       }
}

conn.Close();

 

posted @ 2014-09-17 13:19  xachary  阅读(2400)  评论(0编辑  收藏  举报