2017-04-17 92 views
1

我目前有一个查询,我从两个不同的数据库中获取数据,这些数据库被附加到名为“accountbuys”的一个列表中。从C#中的两个数据库查询数据

  1. 我的第一台具有想买股票三个数据录入(3个帐户

  2. 有17个数据点(17只股票下表买)

我合并这些表和获得输出enter image description here

但是,我想要的输出,应该有17个数据点每次重复3个不同的帐户,以便我们结束呃可以钻取并购买他想要的任何股票enter image description here

PS:如果我想问的问题不清楚,请礼貌地告诉我。请尽量不要粗鲁,我仍然在学习和新的StackExchange!

这是我的代码:`

public List<BuySellModel> GetBuyDataWthAccount() 
{ 
    List<BuySellModel> accountbuys = new List<BuySellModel>(); 

    using (SqlConnection connectionreit = new SqlConnection(HubConnection)) 
    using (SqlConnection connection = new SqlConnection(PMConnection)) 

    { 

     connectionhub.Open(); 
     connection.Open(); 

     SqlCommand command3 = new SqlCommand(@"SELECT distinct(table1.name) as 'Symbol' ,table2.Segment as 'Segment',table2.AllocationAmount as 'AllocationAmount',table2.PX_LAST as 'Price', 
                table1.CUR_MKT_CAP as 'CMC',table1.FCFY_WITH_CUR_MKT_CAP as 'FCMC',table1.ROIC as 'ROIC', table1.ROICDELTA as 'ROICD' FROM View_REIT_Model_And_Holdings as table1 
                INNER JOIN [MostRecentlyInModelSelected] as table2 
                 ON table1.name = table2.Ticker 
                 WHERE table1.AllocationAmount != -1 AND 
                 NOT EXISTS (SELECT NULL FROM [ViewPCData] as table3 WHERE table1.name = table3.Symbol AND table2.Segment = table3.SubsectorDescription AND table3.Objective = 'REITS' AND table3.SectorDescription != 'NULL' AND table3.SubsectorDescription != 'NULL')", 
                connectionreit); 
     command3.CommandType = CommandType.Text; 

     SqlCommand command4 = new SqlCommand("SELECT PortfolioAccountNumber, PortfolioDescription, SUM(TotalValue) as 'TotalValue' FROM [ViewPCData] WHERE Objective = 'REITS' GROUP BY PortfolioAccountNumber,PortfolioDescription", connection); 
     command4.CommandType = CommandType.Text; 

     var reader = command3.ExecuteReader(); 
     var reader1 = command4.ExecuteReader(); 

     if (reader1.HasRows) 
     { 
      while (reader1.Read()) 
      { 
       BuySellModel accountb = new BuySellModel(); 
       accountb.PortfolioAccount = reader1.GetString(reader1.GetOrdinal("PortfolioAccountNumber")); 
       accountb.PortfolioDescription = reader1.GetString(reader1.GetOrdinal("PortfolioDescription")); 
       accountb.AccountAmount = reader1.GetDecimal(reader1.GetOrdinal("TotalValue")); 
       accountbuys.Add(accountb); 

       if (reader.HasRows) 
       { 
        //foreach(var account in accountbuys) 
        //{ 


        while (reader.Read()) 
        { 
         BuySellModel buy = new BuySellModel(); 
         buy.Symbol = reader.GetString(reader.GetOrdinal("Symbol")); 
         buy.Segment = reader.GetString(reader.GetOrdinal("Segment")); 
         //if (accountNumber == "soand os") 
         //{ 
         // 1/3 of totalaccountvalue 
         buy.AllocationAmount = (reader.GetDouble(reader.GetOrdinal("AllocationAmount"))); 
         //} 
         buy.Price = reader.GetDouble(reader.GetOrdinal("Price")); 
         buy.MarketValue = reader.GetDouble(reader.GetOrdinal("CMC")); 
         buy.FCFY = reader.GetDouble(reader.GetOrdinal("FCMC")); 
         buy.ROIC = reader.GetDouble(reader.GetOrdinal("ROIC")); 
         buy.ROICdelta = reader.GetDouble(reader.GetOrdinal("ROICD")); 
         buy.Buy = true; 
         //account1 = account.accountnumber;     
         accountbuys.Add(buy); 

        } 
        //} //for loop 


       } 


      } // accounts 
     } //reader1.hasrows 

     connectionhub.Close(); 
     connection.Close(); 
    } 

    return accountbuys; 
} 

编辑:

分割表分为两个不同的列表,后来它们合并。现在运行良好。看起来也适合缩放。

public List<BuySellModel> GetBuyDataWthAccount() 
    { 
     List<BuySellModel> accountbuys = new List<BuySellModel>(); 

     List<Account> accounts = new List<Account>(); 


     using (SqlConnection connection = new SqlConnection(PMConnection)) 

     { 

      connection.Open(); 

      SqlCommand command3 = new SqlCommand(@"SELECT distinct(table1.name) as 'Symbol' ,table2.Segment as 'Segment',table2.AllocationAmount as 'AllocationAmount',table2.PX_LAST as 'Price', 
               table1.CUR_MKT_CAP as 'CMC',table1.FCFY_WITH_CUR_MKT_CAP as 'FCMC',table1.ROIC as 'ROIC', table1.ROICDELTA as 'ROICD' FROM View_Model_And_Holdings as table1 
               INNER JOIN [MostRecentlyInModelSelected] as table2 
                ON table1.name = table2.Ticker 
                WHERE table1.AllocationAmount != -1 AND 
                NOT EXISTS (SELECT NULL FROM [ViewPCData] as table3 WHERE table1.name = table3.Symbol AND table2.Segment = table3.SubsectorDescription AND table3.Objective = 'STOCKS' AND table3.SectorDescription != 'NULL' AND table3.SubsectorDescription != 'NULL')", 
               connectionreit); 
      command3.CommandType = CommandType.Text; 

      SqlCommand command4 = new SqlCommand("SELECT PortfolioDetail , SUM(TotalValue) as 'TotalValue' FROM [ViewPCData] WHERE Objective = 'STOCKS' GROUP BY PortfolioAccountNumber,PortfolioDescription", connection); 
      command4.CommandType = CommandType.Text; 

      var reader = command3.ExecuteReader(); 
      var reader1 = command4.ExecuteReader(); 
      if (reader1.HasRows) 
      { 
       while (reader1.Read()) 
       { 
        Account accountb = new Account(); 
        accountb.PortfolioDetail = reader1.GetString(reader1.GetOrdinal("PortfolioDetail")); 
        // accountb.PortfolioDescription = reader1.GetString(reader1.GetOrdinal("PortfolioDescription")); 
        accountb.AccountAmount = reader1.GetDecimal(reader1.GetOrdinal("TotalValue")); 

        accounts.Add(accountb); 
       } 
      } 
      //List<BuyReits> buys = new List<BuyReits>(); 
      if (reader.HasRows && accounts.Count > 0) 
      { 
       while (reader.Read()) 
       { 
        foreach (var acc in accounts) 
        { 
         BuySellModel buy = new BuySellModel(); 
         buy.Symbol = reader.GetString(reader.GetOrdinal("Symbol")); 
         buy.Segment = reader.GetString(reader.GetOrdinal("Segment")); 
         buy.AllocationAmount = (reader.GetDouble(reader.GetOrdinal("AllocationAmount"))); 
         buy.Price = reader.GetDouble(reader.GetOrdinal("Price")); 
         //buy.Quantity = reader.GetInt32((reader.GetOrdinal("AllocationAmount"))/(reader.GetOrdinal("Price"))); 
         buy.MarketValue = reader.GetDouble(reader.GetOrdinal("CMC")); 
         buy.FCFY = reader.GetDouble(reader.GetOrdinal("FCMC")); 
         buy.ROIC = reader.GetDouble(reader.GetOrdinal("ROIC")); 
         buy.ROICdelta = reader.GetDouble(reader.GetOrdinal("ROICD")); 
         buy.Buy = true; 
         buy.PortfolioAccount = acc.PortfolioDetail; 
         buy.AccountAmount = acc.AccountAmount; 

         accountbuys.Add(buy); 

        } 

       } 
      } 

      connection.Close(); 


     } 

     return accountbuys; 

    } 
+0

你使用的是mysql还是sql server?它们不是同一件事。此外,如果您发布了有关表格的一些详细信息,这将非常有帮助。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –

+0

通常会询问您是否确切指出您目前无法工作的内容码。你也有标签为MySQL和SQL服务器,它通常要求你选择一个。 –

+0

不是很清楚什么是不工作。有什么问题? –

回答

0

不改变任何你的C#代码,它可能可以得到你想要的东西通过从内部改变SQL查询联接查询到CROSS JOIN查询。

虽然关于这种方法的一些注意事项:

  1. 如果包括WHERE子句,该查询将作为一个INNER JOIN。

  2. 交叉连接可能会明显变慢,因为查询正在寻找所有可能的组合。由于只有51(3 * 17)种组合方式,这将不会是一个显著的问题,但如果你正在寻找扩展到广大的客户和股票,将成为性能越来越差明智

+0

是的,我正在寻找规模,因为我可能有更多的客户。 – ForeverLearner

1

下面提供的横在C#层连接(不是说这可能是最好的解决方案,但它让你更接近准备好):

using (SqlConnection connectionhub = new SqlConnection(HubConnection)) 
using (SqlConnection connection = new SqlConnection(PMConnection)) 

{ 

    connectionhub.Open(); 
    connection.Open(); 

    SqlCommand command3 = new SqlCommand(@" 
     SELECT distinct(table1.name) as 'Symbol', 
       table2.Segment as 'Segment', 
       table2.AllocationAmount as 'AllocationAmount', 
       table2.PX_LAST as 'Price', 
       table1.CUR_MKT_CAP as 'CMC', 
       table1.FCFY_WITH_CUR_MKT_CAP as 'FCMC', 
       table1.ROIC as 'ROIC', 
       table1.ROICDELTA as 'ROICD' 
      FROM View_REIT_Model_And_Holdings as table1 
       INNER JOIN [MostRecentlyInModelSelected] as table2 
        ON table1.name = table2.Ticker 
     WHERE table1.AllocationAmount != -1 
      AND NOT EXISTS (SELECT NULL 
          FROM [ViewPCData] as table3 
          WHERE table1.name = table3.Symbol 
           AND table2.Segment = table3.SubsectorDescription 
           AND table3.Objective = 'REITS' 
           AND table3.SectorDescription != 'NULL' 
           AND table3.SubsectorDescription != 'NULL')", 
               connectionreit); 
    command3.CommandType = CommandType.Text; 

    SqlCommand command4 = new SqlCommand(@" 
     SELECT PortfolioAccountNumber, 
       PortfolioDescription, 
       SUM(TotalValue) as 'TotalValue' 
      FROM [ViewPCData] 
     WHERE Objective = 'REITS' 
     GROUP BY PortfolioAccountNumber, PortfolioDescription", connection); 
    command4.CommandType = CommandType.Text; 

    var stocksDS = new DataSet(); 
    var stocksDA = new System.Data.SqlClient.SqlDataAdapter(); 
    stocksDA.SelectCommand = command3 
    stocksDA.Fill(stocksDS, "stocks"); 

    var acctsDS = new DataSet(); 
    var acctsDA = new System.Data.SqlClient.SqlDataAdapter(); 
    acctsDA.SelectCommand = command4 
    acctsDA.Fill(acctsDS, "accts"); 

    var stocks = stocksDS.Tables["stocks"].AsEnumerable(); 
    var accts = acctsDS.Tables["accts"].AsEnumerable(); 

    var results = (from stocksDR in stocks 
        from acctsDR in accts 
        select new BuySellModel { 
         PortfolioAccount = acctsDR["PortfolioAccountNumber"], 
         PortfolioDescription = acctsDR["PortfolioAccountDescription"], 
         AccountAmount = acctsDR["TotalValue"], 
         Symbol = stocksDR["Symbol"], 
         Segment = stocksDR["Segment"], 
         AllocationAmount = stocksDR["AllocationAmount"], 
         Price = stocksDR["Price"], 
         MarketValue = stocksDR["CMC"], 
         FCFY = stocksDR["FCMC"], 
         ROIC = stocksDR["ROIC"], 
         ROICdelta = stocksDR["ROICD"], 
         Buy = true 
        }); 

    foreach (BySellModel buy in results) { 
     accountBuys.Add(buy); 
    } 

    connectionhub.Close(); 
    connection.Close(); 
} 

编辑:删除违规括号。

+0

谢谢!我得到一个错误,“在System.Data.dll中发生类型'System.ArgumentException'的异常,但没有在用户代码中处理”我是否缺少某些东西?我确实有“ConfigurationManager.ConnectionStrings”设置。 – ForeverLearner

+0

它报告错误的哪一行?另外,您是否在文件的顶部添加了'使用System.Data.DataSetExtensions;'?您可能还需要将对DLL的引用添加到项目中。 – Forty3

+0

它向整个“Select new BuySellModel()”块提供错误信息给var结果的末尾。 它也给了这个 - “附加信息:列'PortfolioAccountDescription'不属于表accts。” – ForeverLearner