直接从SQL Server填充observableCollection [英] Fill observableCollection directly from sql server
本文介绍了直接从SQL Server填充observableCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有任何方法可以直接从SQL Server填充ObservableCollection. 当前,我正在将数据加载到DataTable中(使用sqlDataAdapter),并手动进行到ObservableCollection的转换,这效率很低.
I would like to know if there is any way to fill ObservableCollection directly from SQL server. Currently I am loading my data into DataTable (using sqlDataAdapter) and do a conversion to ObservableCollection manually and it very inefficient.
如何做到?
推荐答案
好的,我找到了一种方法:
OK, I have found a way to do this:
可以使用SqlDataReader完成.
It can be done using SqlDataReader.
public ObservableCollection<DataItem> LoadCategoriesData()
{
Command = new SqlCommand(StoredProcedure, Connection);
Command.CommandType = CommandType.StoredProcedure;
ObservableCollection<DataItem> myColl = new ObservableCollection<DataItem>();
Connection.Open();
SqlDataReader reader = Command.ExecuteReader();
while (reader.Read())
{
int mainCatID = reader.GetInt32(0);
string categoryName = reader.GetString(1);
//adding row data to observable
myColl.Add(new DataItem(mainCatID, categoryName));
}
Connection.Close();
return myColl;
}
这篇关于直接从SQL Server填充observableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文