https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.list-1?view=netframework-4.8
MySQLHelper
/// <summary> /// 查询_返回一组数 /// </summary> /// <param name="str"></param> /// <returns></returns> public static List<string> selectSQL(string str) { try { conn.Open(); //建立MySQL连接 MySqlCommand cmd = new MySqlCommand(str, conn); //执行 MySqlDataReader reader = cmd.ExecuteReader(); //读取数组 List<string> strrr = new List<string>(); while (reader.Read()) { strrr.Add(reader[0].ToString()); } conn.Close(); //关闭MySQL连接 return strrr; } catch (Exception e) { conn.Close(); //关闭MySQL连接 List<string> list = new List<string>(); list.Add("执行SQL语句时出错啦!关闭连接成功" + e.ToString()); return list; } }
这个MySQL Helper类参考了https://bbs.csdn.net/topics/250005815;
7楼时对的,8楼时错的。。。。。
调用方法
List<string> adp = MySqlHelper.selectSQL("select name from perinfo;"); MessageBox.Show(adp.Count.ToString());