private IList<CustomersM> GetAllCustomers()
{
IList<CustomersM> customer = new List<CustomersM>();
using (sqlConnection connection = new sqlConnection(ConfigurationManager.ConnectionStrings["cc_2005"].ConnectionString))
{
connection.open();
sqlCommand command = new sqlCommand("SELECT [CompanyName],[ContactName],[ContactTitle],[Address],[City] FROM [Customers]",connection);
sqlDataReader sdr=command.ExecuteReader(CommandBehavior.CloseConnection);
while (sdr.Read())
{
/*CustomersM m = new CustomersM();
m.CompanyName = sdr[0].ToString();
m.ContactName = sdr[1].ToString();
m.ContactTitle = sdr[2].ToString();
m.Address = sdr[3].ToString();
m.City = sdr[4].ToString();
*
*/
CustomersM m = new CustomersM(sdr[0].ToString(),sdr[1].ToString(),sdr[2].ToString(),sdr[3].ToString(),sdr[4].ToString());
customer.Add(m);
}
return customer;
}
}
3.这样GetAllCustomers就可以绑定到GridView,DropDownList等控件上了。
注意:IList是不能序列化的,因此当需要序列化的时候(如:webservice,由于webservice中传递的对象都是可以序列化的)必须使用System.Collections.ObjectModel.Collection< >
代码如下:
private System.Collections.ObjectModel.Collection<CustomersM> GetCustomers()
{
System.Collections.ObjectModel.Collection<CustomersM> customer = new System.Collections.ObjectModel.Collection<CustomersM>();
using (sqlConnection connection = new sqlConnection(ConfigurationManager.ConnectionStrings["cc_2005"].ConnectionString))
{
connection.open();
sqlCommand command = new sqlCommand("SELECT [CompanyName],connection);
sqlDataReader sdr = command.ExecuteReader(CommandBehavior.CloseConnection);
while (sdr.Read())
{
CustomersM m = new CustomersM(sdr[0].ToString(),sdr[4].ToString());
customer.Add(m);
}
return customer;
}
}
在webservice中如果把上面两个调用数据库的方法搬过去(好多命名空间要添加),使用IList<>类型的就会报错,“IList不能序列化接口”,而使用Collection<>的就可以正常使用.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。