WPF_MySQL查询结果存入数组
时间:2021-07-01 10:21:17
帮助过:24人阅读
<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());
WPF_MySQL查询结果存入数组
标签:help show except read ros box mes cmd pen