2011-12-29 7 views
0

からジェネリックメソッドの戻り値の一覧 は、私がモデルを持っていると仮定し データベース

person(integer ID, char Name) 

は、どのように私は、データベースからときでのIEnumerable(またはリスト)を返すことができますコンパイラ時間モデル(Person)は不明です。

public List<object> Select(string query, Dictionary<string, object> parameters) 
     { 
      List<object> listValue = new List<object>(); 
      using (var command = new MySqlCommand(query, connection)) 
      { 
       #region fill command.Parameters.AddWithValue 
       if (parameters != null) 
       { 
        foreach (var param in parameters) 
        { 
         if (param.Value == null) 
         { 
          command.Parameters.AddWithValue("@" + param.Key, DBNull.Value); 
          continue; 
         } 
         DateTime _date = new DateTime(); 
         if (DateTime.TryParse(param.Value.ToString(), out _date)) 
         { 
          command.Parameters.AddWithValue("@" + param.Key, _date.ToString("yyyy-MM-dd")); 
          continue; 
         } 
         command.Parameters.AddWithValue("@" + param.Key, param.Value); 
        } 
       } 
       #endregion 

       if (connection.State == System.Data.ConnectionState.Closed) connection.Open(); 
       try 
       { 
        using (MySqlDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection)) 
        { 
         while (reader.Read()) 
         { 
          object newObject = new object(); 

          // What must I do there? 

          listValue.Add(new { }); 
         } 
        } 
       } 
       catch (Exception ex) { } 
       finally 
       { 
        if (connection.State == System.Data.ConnectionState.Open) 
         connection.Close(); 
       } 
      } 
      return listValue; 
} 
+3

'にデータベースからデータローを変換する方法を決定できるようにする「FUNC」のような新しい引数が必要だと思う:例えば、私が思うに、selectメソッドの必須の構造は次の通りでありますcatch(Exception ex){} ' - サイドノート、これは悪い考えです。なぜ何かが失敗したのかも知りたくないのですが?例外には、問題を解決するための非常に有用な情報が含まれている傾向があります。 – David

+1

それはコンパイルの一部ではないのですか?コンパイルに含まれていない場合、モデルはありません.SQLデータベースのみを読み込みたいのですが、そこから簡単に解決できます。 – Euphoric

+0

一般的なSelectモデルfまたは私のモデル(Person、Customer ...) –

答えて

0

私はuは呼び出し元がエンティティ

関連する問題