c mysql dataset查询_MySql查询数据

引用的DLL

MySql.Data.MySqlClient

System.Data

City实体

public class City

{

public int ID { get; set; }

public string Name { get; set; }

public string CountryCode { get; set; }

public string District { get; set; }

public int Population { get; set; }

}

连接字符串

string connectionStr = "server=127.0.0.1;database=数据库;User ID=root;password=密码";

实现逻辑

public List GetCityList()

{

List cityList = new List();

string sqlStr = "select * from city";

using (MySqlConnection con = new MySqlConnection(connectionStr))

{

con.Open();

MySqlCommand command = new MySqlCommand();

if (con.State != ConnectionState.Open)

{

con.Open();

}

command.Connection = con;

command.CommandText = sqlStr;

using (MySqlDataAdapter da = new MySqlDataAdapter(command))

{

DataSet ds = new DataSet();

da.Fill(ds, "city");

foreach (DataRow inst in ds.Tables[0].Rows)

{

City city = new City();

city.ID = int.Parse(inst["ID"].ToString());

city.Name = inst["Name"].ToString();

city.CountryCode = inst["CountryCode"].ToString();

city.District = inst["District"].ToString();

city.Population = int.Parse(inst["Population"].ToString());

cityList.Add(city);

}

}

}

return cityList;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值