class Program { static void Main(string[] args) { DataTable dt = new DataTable(); using( MySqlConnection conn = new MySqlConnection("Server=localhost; Database=study; uid=root; pwd=root; Charset=utf8") ) using (MySqlCommand cmd = conn.CreateCommand()) { conn.Open(); cmd.CommandText = "select * from t_customers"; //读取数据 using (MySqlDataReader reader = cmd.ExecuteReader()) { //把数据加载到DataTable中 dt.Load(reader); } } //遍历数据 for (int i = 0; i < dt.Rows.Count; i++) { DataRow row = dt.Rows[i]; int id = (int)row["Id"]; string name = (string)row["Name"]; Console.WriteLine(id+name); } Console.ReadKey(); } }
时间: 2024-11-04 13:50:14