SQL Server で LLBLGen 3.1 (セルフ サービス) を使用して、次のようなカスタム SQL を実行するにはどうすればよいでしょうか。
- UserPreference から削除
- select * from UserPreference (たとえば、データテーブルに)
SQL Server で LLBLGen 3.1 (セルフ サービス) を使用して、次のようなカスタム SQL を実行するにはどうすればよいでしょうか。
この質問に答えられていないことに気付きました。セルフ サービスでは、おそらく TypedListDAO クラスを使用します。
参照:生成されたコード - DataReaders とプロジェクションのフェッチ、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);
または、micro-orm を使用して SQL を実行し、Dapper (1 つの cs ファイル)、PetaPoco、Massive などの軽量な micro-orms の上の TypedListDAO クラスからの接続を使用するだけです。