要从Firebird基础获取我的数据(用户和书籍),我使用以下方法:
public static Dictionary<string, Users> getUsersFromDB()
{
Dictionary<string, Users> users_list = new Dictionary<string, Users>();
string reqestCommand = "SELECT id, user_type, user_nick, user_pass, user_fname, user_lname, user_identify, e_mail,address, registered FROM USERS";
string connectionString = connectionPath();
using (FbConnection fbDB = new FbConnection(connectionString))
{
fbDB.Open();
FbCommand reqest = new FbCommand(reqestCommand, fbDB);
using (FbDataReader reader = reqest.ExecuteReader())
{
while (reader.Read())
{
string key;
Users user = new Users();
user.ID = reader.GetInt32(0);
user.type = reader.GetString(1).Trim();
key = reader.GetString(2).Trim();
user.nick = key;
user.password = reader.GetString(3).Trim();
user.fName = reader.GetString(4).Trim();
user.lName = reader.GetString(5).Trim();
user.identify = reader.GetString(6).Trim();
user.email = reader.GetString(7).Trim();
user.address = reader.GetString(8).Trim();
user.registerDate = reader.GetString(9).Trim();
user.localOrInDb = "inDatabase";
users_list.Add(key, user);
}
}
fbDB.Close();
}
return users_list;
}
public static Dictionary<Guid, Books> getBooksFromDB()
{
Dictionary<Guid, Books> books_list = new Dictionary<Guid, Books>();
string reqestCommand = "SELECT book_id, book_title, book_author, book_genre, book_language, book_rel_date, book_page_number, book_available_amount, book_type FROM BOOKS";
string connectionString = connectionPath();
using (FbConnection fbDB = new FbConnection(connectionString))
{
fbDB.Open();
FbCommand reqest = new FbCommand(reqestCommand, fbDB);
using (FbDataReader reader = reqest.ExecuteReader())
{
while (reader.Read())
{
Guid key;
Books book = new Books();
Guid theGuid = new Guid(reader.GetString(0).Trim());
key = book.ID = theGuid;
book.title = reader.GetString(1).Trim();
book.author = reader.GetString(2).Trim();
book.genre = reader.GetString(3).Trim();
book.language = reader.GetString(4).Trim();
book.rel_date = reader.GetString(5).Trim();
book.page_number = reader.GetInt32(6);
book.amount = reader.GetInt32(7);
book.type = reader.GetString(8).Trim();
book.localOrInDb = "inDatabase";
books_list.Add(key, book);
}
}
fbDB.Close();
}
return books_list;
}
我们怎么看,它们几乎相同,所以问题:
是否有可能从中获得一项功能?或者将它们分开会更好吗?
答案 0 :(得分:2)
您可以传递一个参数,该参数构成目标类的实例,来自Reader
:
public static Dictionary<K,T> GetFromDb<K,T>(Func<Reader,KeyValuePair<K,T> maker) {
var res = new Dictionary<K,T>();
// ... your code ...
while (reader.Read()) {
res.Add(maker(reader));
}
return res;
}
现在你可以这样打电话给你GetFromDb
:
var users = GetFromDb<string,User>(reader => {
string key;
Users user = new Users();
user.ID = reader.GetInt32(0);
user.type = reader.GetString(1).Trim();
key = reader.GetString(2).Trim();
user.nick = key;
user.password = reader.GetString(3).Trim();
user.fName = reader.GetString(4).Trim();
user.lName = reader.GetString(5).Trim();
user.identify = reader.GetString(6).Trim();
user.email = reader.GetString(7).Trim();
user.address = reader.GetString(8).Trim();
user.registerDate = reader.GetString(9).Trim();
user.localOrInDb = "inDatabase";
return new KeyValuePair<string,User>(key, user);
});
答案 1 :(得分:1)
查看此帖子
https://stackoverflow.com/a/1464929/988830
您可以使用扩展方法抽象阅读器。
但是,您必须每次都创建FBconnection。如果你试图将它放在一个函数中并从另一个函数中访问它,那么你会得到读者关闭的异常。
使用IDisposible http://msdn.microsoft.com/en-us/library/system.idisposable.aspx 它关闭了连接。
另一个选项 开始使用orm之类的Entity框架或NHibernate,它将使用您的对象映射表。