我创建了以下内容来创建User对象,其中包含名为UserModules的UserModule对象的ObservableCollection。
我有约。 100000个用户记录,每个用户最多可以有10个模块记录,这需要几分钟才能完成。
这可能意味着从IDataReader改为开放式建议。有人可以提出一种更有效的方法吗?
public void LoadUsers()
{
clsDAL.SQLDBAccess db = new clsDAL.SQLDBAccess("USERS");
clsDAL.SQLDBAccess db_user_modules = new clsDAL.SQLDBAccess("USERS");
try
{
db.setCommandText(@"SELECT * FROM Users");
using (var reader = db.ExecuteReader())
{
while (reader.Read())
{
var user = new User();
MapUser(reader, user);
_users.Add(user);
db_user_modules.setCommandText(@"SELECT MODULE_ID, USER_MODULE_ACCESS FROM USER_MODULE_SECURITY Where USER_ID = " + user.User_ID);
using (var reader_user_modules = db_user_modules.ExecuteReader())
{
while (reader_user_modules.Read())
{
MapUserModule(reader_user_modules, user);
}
}
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
throw;
}
finally
{
db = null;
}
}
MapUser
public static void MapUser(IDataRecord record, User user)
{
try
{
user.User_ID = NullSafeGetter.GetValueOrDefault<int>(record, "USER_ID");
user.Username = NullSafeGetter.GetValueOrDefault<string>(record, "USERNAME");
user.Name = NullSafeGetter.GetValueOrDefault<string>(record, "NAME");
user.Job_Title = NullSafeGetter.GetValueOrDefault<string>(record, "JOB_TITLE");
user.Department = NullSafeGetter.GetValueOrDefault<string>(record, "DEPARTMENT");
user.Company = NullSafeGetter.GetValueOrDefault<string>(record, "COMPANY");
user.Phone_Office = NullSafeGetter.GetValueOrDefault<string>(record, "PHONE_OFFICE");
user.Phone_Mobile = NullSafeGetter.GetValueOrDefault<string>(record, "PHONE_MOBILE");
user.Email = NullSafeGetter.GetValueOrDefault<string>(record, "EMAIL");
user.Password = NullSafeGetter.GetValueOrDefault<string>(record, "PASSWORD");
user.Last_Login = NullSafeGetter.GetValueOrDefault<DateTime>(record, "LAST_LOGIN");
user.Status = NullSafeGetter.GetValueOrDefault<int>(record, "STATUS");
user.Session_Timeout = NullSafeGetter.GetValueOrDefault<int>(record, "SESSION_TIMEOUT");
}
catch (Exception ex)
{
MessageBox.Show("Mapping User error: " + ex.Message);
throw;
}
}
MapUserModule
private static void MapUserModule(IDataRecord record, User user)
{
try
{
int m_id = NullSafeGetter.GetValueOrDefault<int>(record, "MODULE_ID");
int uma = NullSafeGetter.GetValueOrDefault<int>(record, "USER_MODULE_ACCESS");
user.UserModules.Add(new Users.UserModule(user.User_ID, m_id, uma));
}
catch (Exception ex)
{
throw new Exception("Mapping UserModule error:\n" + ex.Message);
}
}
答案 0 :(得分:3)
public IEnumerable<UserModule> GetUserModules()
{
using(var db = ....)
db.setCommandText("SELECT * FROM USERMODULES");
using (var reader = db.ExecuteReader())
{
while (reader.Read())
{
var userId = reader[...];
var m_id = reader[...];
var uma = reader[...];
yield return new UserModule (userid, m_id, uma)
}
}
}
public IEnumerable<User> GetUsers()
{
var userModulesLookup = GetUserModules().ToLookup(x => x.UserId);
using (var db = ...)
{
db.setCommandText("SELECT * FROM USERS");
using (var reader = db.ExecuteReader())
{
while (reader.Read())
{
var userId = reader["userId"];
...blah blah blah...
var user = return new User();
user.Modules = new ObservableCollection<UserModule>
(userModulesLookup[userId]);
...blah blah blah...
yield return user;
}
}
}
}
public void LoadUsers()
{
var users = GetUsers();
foreach(var u in users)
_users.Add(u);
}
答案 1 :(得分:1)
据我所知,没有比使用DataReader更快的解决方案。
我建议您对代码进行分析,以查看大部分时间占用的内容。 IIRC,一次向一个可观察的集合中添加大量项目的速度很慢。尝试将它们添加到List&lt;&gt;暂时试图找出问题。