2011-10-19 67 views

回答

1

剛注意到這個問題還沒有得到解答。通過自我服務,您可能會使用TypedListDAO類。

參見:Generated code - Fetching DataReaders and projections, SelfServicing

的TypedListDAO類有你需要做的SQL對數據庫的東西,如果你需要(見文章),它可以自動爲您做預測到自定義類。

但基本上,(從內存中,所以可能需要一些輕微的調整),這裏是你的代碼可能是什麼樣子:

 // inside the DaoClasses namespace of your generated project 
     TypedListDAO dao = new TypedListDAO(); 

     // do it yourself, and use your project's connection string 
     string connectionString = CommonDaoBase.ActualConnectionString; 
     using (var conn = new SqlConnection(connectionString)) { } 

     // use a DbConnection directly 
     DbConnection connection = dao.CreateConnection(); 
     // or 
     connection = dao.DetermineConnectionToUse(null); 
     DbCommand cmd = connection.CreateCommand(); 
     cmd.CommandText = "SELECT * FROM UserPreferences"; 
     cmd.CommandType = CommandType.Text; 
     var reader = cmd.ExecuteReader(CommandBehavior.Default); 
     while (reader.Read()){} 
     reader.Close(); 

     // use a datareader 
     IRetrievalQuery query = new RetrievalQuery(
      new SqlCommand("SELECT * FROM UserPreferences")); 
      // or new RetrievalQuery(cmd); 
      // where you create the cmd using the dao connection 

     IDataReader reader = dao.GetAsDataReader(null, query,  
      CommandBehavior.CloseConnection); 
     while (reader.Read()){} 
     reader.Close(); 

     // use a datatable - try something like this 
     // (BUT honestly, you might want to look at the custom projection 
     // into custom classes capability, or the data reader, instead of this) 
     DataTable dt = new DataTable(); 
     dao.GetMultiAsDataTable(new EntityFields(0) /* can't be null, i don't think */, 
       dt, query, null); 

     // other methods 
     dao.ExecuteScalarQuery(query, null); 
     ActionQuery actionQuery = new ActionQuery(new SqlCommand("INSERT ...")); 
     dao.ExecuteActionQuery(actionQuery, null); 

或者使用微ORM做你的SQL語句,只需使用來自TypedListDAO類的連接超過 某些輕量級微型操作系統,如:Dapper(1 cs文件),PetaPoco,Massive等...

0

雖然確實可以訪問低級數據讀取器,等等。我認爲這有點違背了使用ORM的目的。如果您只想從集合中填充數據表(可以使用或不使用篩選),則可以使用靜態方法GetMultiAsDataTable(如果要進行篩選,則可以將謂詞表達式傳遞給它)。如果您想更換更復雜的SQL(報告非常有用),檢查出的動態列表功能:

http://www.llblgen.com/documentation/4.0/LLBLGen%20Pro%20RTF/hh_start.htm

的QuerySpec是指定一個動態查詢和項目它甚至更好的方式:

http://www.llblgen.com/documentation/4.0/LLBLGen%20Pro%20RTF/hh_start.htm