代码如下:
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], [ContactName], [ContactTitle], [Address], [City] FROM [Customers]", connection);
SqlDataReader sdr = command.ExecuteReader(CommandBehavior.CloseConnection);
while (sdr.Read())
{
CustomersM m = new CustomersM(sdr[0].ToString(), sdr.ToString(), sdr.ToString(), sdr.ToString(), sdr.ToString());
customer.Add(m);
}
return customer;
}
}
友情提示:
在webservice中如果把上面两个调用数据库的方法搬过去(好多命名空间要添加),使用IList<>类型的就会报错,“IList不能序列化接口”,而使用Collection<>的就可以正常使用,这一点要记住了!
我总结的不对的地方请指出,谢谢!