WPF_MySQL查询结果存入数组

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());
posted @ 2019-12-30 16:42  王钰  阅读(523)  评论(0编辑  收藏  举报