功能问题3

时间:2016-04-18 18:54:00

标签: c# .net sql-server

我遇到了问题,我在数据库中有group BY制作了GraficoCor,并且有另一个表While,我每次都IdCor表示函数使增量传递给第二个函数的变量IdCor的计数器值,第二个函数将查询具有public static FatoFaturamentoIVELBO[] GetFaturamentoIVEL(string Operacao, Connection Cn) { var RsFaturamento = new Recordset(); int Cont = 0; try { RsFaturamento.Open(String.Format("SELECT Operacao, AnoMes, TradeMarketing, SUM(ValorNF)AS ValorTotal FROM dbo.FatoFaturamentoIVEL WHERE TradeMarketing = 0 and AnoMes = '2016/04' GROUP BY Operacao, AnoMes, TradeMarketing ORDER BY SUM(ValorNF) ASC", Operacao), Cn, CursorTypeEnum.adOpenStatic, LockTypeEnum.adLockReadOnly); var ArrayRetorno = new FatoFaturamentoIVELBO[RsFaturamento.RecordCount]; while (!RsFaturamento.EOF) { FatoFaturamentoIVELBO Faturamento = new FatoFaturamentoIVELBO(); Faturamento.Operacao = RsFaturamento.Fields["Operacao"].Value.ToString(); Faturamento.AnoMes = RsFaturamento.Fields["AnoMes"].Value.ToString(); Faturamento.ValorNF = decimal.Parse(RsFaturamento.Fields["ValorTotal"].Value.ToString()); ArrayRetorno[Cont] = Faturamento; Cont++; RsFaturamento.MoveNext(); } RsFaturamento.Close(); return ArrayRetorno; } catch (Exception ex) { throw new Exception("Erro: " + ex.Message); } } 的值作为参考,返回第一个函数的十六进制颜色。 怎么做?

功能GetFaturamentoIVEL

public  static FatoFaturamentoIVELBO GetCor(int IdCor, Connection Cn)
        {
            var Cor = new FatoFaturamentoIVELBO();
            var RsCor = new Recordset();
            try
            {
                RsCor.Open(String.Format("SELECT IdCor, CodHex from dbo.GraficoCor  where IdCor = " + IdCor), Cn, CursorTypeEnum.adOpenStatic, LockTypeEnum.adLockReadOnly);
                if (!RsCor.EOF)
                {
                    Cor.CodHex = RsCor.Fields["CodHex"].Value.ToString();
                }
                return Cor;
            }
            catch (Exception ex)
            {
                throw new Exception("Erro :" + ex.Message);
            }
        }

功能GetCor

{{1}}

2 个答案:

答案 0 :(得分:0)

据推测,你只需要打电话:

Faturamento.SomeProperty = GetCor(Cont, Cn)

由于两个原因,这不太可能是你想要的。使用这样的计数器生成数据库ID通常是错误的,而id应该作为查询的一部分返回。而不是为每一行单独请求,您应该一起加入查询并一次性获取数据。参见例如this question

答案 1 :(得分:0)

我猜你主要说另一种语言 - 这很好,而不是你的错 - 但是很难理解你在问什么。你能编辑你写的内容并用不同的方式说出来吗?

这是一个总猜测,但我认为你需要重写你的第一个查询:

SELECT Operacao, AnoMes, TradeMarketing, SUM(ValorNF)AS ValorTotal FROM dbo.FatoFaturamentoIVEL WHERE TradeMarketing = 0 and AnoMes = '2016/04' GROUP BY Operacao, AnoMes, TradeMarketing ORDER BY SUM(ValorNF) ASC

因此,INNER JOIN使用LEFT OUTER JOINdbo.GraficoCor,因此您不必每次都通过GetCor循环调用while

我认为您可以进行子选择或类似的事情应该向您展示如何使用ROW_NUMBER()

SELECT 
[Person Name], 
[Row Type]
FROM
(
    SELECT 
    *,
    ROW_NUMBER() OVER(ORDER BY [Person Name] ASC) AS [Id]
    FROM
    (
        SELECT 'Alice' [Person Name] UNION
        SELECT 'Bob' [Person Name] UNION
        SELECT 'Charlie' [Person Name]  UNION
        SELECT 'Daniel' [Person Name] 
    ) [Alias]
) AS [Main]

LEFT OUTER JOIN
(
    SELECT 0 [Parity], 'Even Row' [Row Type] UNION
    SELECT 1 [Parity], 'Odd Row' [Row Type]
) [Lookup]
ON [Main].[Id] % 2 = [Lookup].[Parity]

结果:

Person Name | Row Type
----------------------
Alice       | Odd Row
Bob         | Even Row
Charlie     | Odd Row
Daniel      | Even Row

因此,在您的GetFaturamentoIVEL函数中,您可以执行此操作(未经测试):

RsFaturamento.Open(String.Format("@
SELECT 
[OrderedQuery].[Operacao], 
[OrderedQuery].[AnoMes], 
[OrderedQuery].[TradeMarketing],
[OrderedQuery].[ValorTotal],
[GraficoCor].[CodHex]
FROM
(
    SELECT 
    *,
    (ROW_NUMBER() OVER(ORDER BY SUM(ValorNF) ASC)) - 1 AS [IdCor]
    FROM
    (
        SELECT 
        Operacao, 
        AnoMes, 
        TradeMarketing,
        SUM(ValorNF) AS ValorTotal 
        FROM dbo.FatoFaturamentoIVEL 
        WHERE TradeMarketing = 0 and AnoMes = '2016/04' 
        GROUP BY Operacao, AnoMes, TradeMarketing           
    ) [Query]
) AS [OrderedQuery]    
LEFT OUTER JOIN [dbo].[GraficoCor] [GraficoCor] ON [OrderedQuery].[IdCor] = [GraficoCor].[IdCor]
", Operacao), Cn, CursorTypeEnum.adOpenStatic, LockTypeEnum.adLockReadOnly);