首页  编辑  

C#和MySQL运行SQL语句并读取行进行处理

Tags: /C#/数据库和SQL语句/   Date Created:
C#, MySQL,运行SQL语句,并按行顺序处理数据:
            MySqlConnection mysql = new MySqlConnection(ConfigurationSettings.AppSettings["Connection"]);
                mysql.Open();
                MySqlCommand cmd = mysql.CreateCommand();
		cmd.CommandText = "sql语句";
                MySqlDataReader rdr = cmd.ExecuteReader();
		while (rdr.Read()) {
   			//处理数据 rdr["字段名"]
		}

读取到DataTable并遍历:
            MySqlConnection mysql = new MySqlConnection(ConfigurationSettings.AppSettings["Connection"]);
                mysql.Open();
                String sql = @"SELECT devID, device_id, devName, deviceName, lng, lat FROM tb_device A, devinfo b WHERE a.`id` = b.`device_id`";
                MySqlDataAdapter msa = new MySqlDataAdapter(sql, mysql);
                DataTable dt = new DataTable();
                msa.Fill(dt);
                foreach (DataRow rdr in dt.Rows)
                {
			//  rdr["字段名"]