/// <summary> /// Command를 실행시켜, 결과 셋을 DataSet에 저장한다.<br/> /// 여러 ResultSet을 반환하는 경우 각각의 ResultSet에 대응하는 TableName을 제공해야 합니다. /// </summary> /// <param name="repository">IAdoRepository 인스턴스</param> /// <param name="cmd">실행할 Command</param> /// <param name="targetDataSet"></param> /// <param name="tableNames"></param> /// <example> /// <code> /// // Region 전체 정보 및 갯수를 가져옵니다. /// string query = "select * from Region; select count(*) from Region"; /// /// DataSet ds = new DataSet(); /// using (DbCommand cmd = Impl.GetSqlStringCommand(query)) /// { /// Impl.LoadDataSet(cmd, ds, new string[] { "Region", "CountOfRegion" }); /// Assert.AreEqual(2, ds.Tables.Count); /// Assert.AreEqual(4, ds.Tables[1].Rows[0][0]); /// } /// </code> /// </example> public static void LoadDataSet(this IAdoRepository repository, DbCommand cmd, DataSet targetDataSet, string[] tableNames) { cmd.ShouldNotBeNull("cmd"); tableNames.ShouldNotBeEmpty("tableNames"); if (IsDebugEnabled) { log.Debug("DbCommand를 실행하여 DataSet에 로등합니다... cmd.CommandText=[{0}], tableNames=[{1}]", cmd.CommandText, tableNames.CollectionToString()); } for (int i = 0; i < tableNames.Length; i++) { tableNames[i].ShouldNotBeWhiteSpace("index=" + i); } if (repository.IsActiveTransaction) { AdoTool.EnlistCommandToActiveTransaction(repository, cmd); } var newConnectionCreated = false; if (cmd.Connection == null) { cmd.Connection = AdoTool.CreateTransactionScopeConnection(repository.Db, ref newConnectionCreated); } using (var adapter = repository.GetDataAdapter()) { for (var j = 0; j < tableNames.Length; j++) { var sourceTable = (j == 0) ? AdoTool.DefaultTableName : (AdoTool.DefaultTableName + j); adapter.TableMappings.Add(sourceTable, tableNames[j]); } adapter.SelectCommand = cmd; adapter.Fill(targetDataSet); if (newConnectionCreated) { AdoTool.ForceCloseConnection(adapter.SelectCommand); } } }