SqlDataReader
特点: 遍历整个数据,只读向一个方向读取一行行,
缺点: 不灵活,只能读取数据量少,并且一直占用连接
读取方式: Read()获取一行的数据,再次调用Read();方法
当调用Read()方法返回的False时,就不在表示有数据行。
创建方式
cmd.ExecuteReader()来创建。 cmd.ExecuteReader(CommandBehaviour.CloseConnection)---好处:关闭reader对象时,就自动关闭连接.
样式:
public void ProcessRequest(HttpContext context) { List<Goods_date> goods_date = new List<Goods_date>(); Mg_all<List<Goods_date>> obj = new Mg_all<List<Goods_date>>(); string result = string.Empty; context.Response.ContentType = "text/plain"; // string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString; string ConnectionString = "数据库"; MySqlConnection conn = new MySqlConnection(ConnectionString); conn.Open(); //Goods_date goods_date = new Goods_date(); string str1 = "Select * from tickets"; MySqlCommand cmd = new MySqlCommand(str1,conn); MySqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { goods_date.Add(new Goods_date { ID = dr.GetString(0),player=dr.GetString(1), officer = dr.GetString(2), time = dr.GetString(3), date = dr.GetString(4), amount = dr.GetString(5), paid = dr.GetString(6) }); } dr.Close(); dr.Dispose(); obj.code ="0"; obj.msg = ""; obj.count = "10"; obj.data = goods_date; result= new JavaScriptSerializer().Serialize(obj); context.Response.Write(result);
}