从??? _ GetDisplayData和GROUP数据中选择DataTable(仅一些值),其中不同列的组规则不同

时间:2010-03-04 08:27:46

标签: c# .net asp.net sql

溢出:3我遇到了一个棘手的问题。

  

所以我会尝试清楚地解释我的想法   我的英语很差:/需要   选择DataTable 某些值   来自 ??? _ GetDisplayData   小组规则的主要月份(GROUP IT)的每一天的程序   不同的列是不同的

我需要从其他存储过程中选择存储过程中的一些值,如下所示:

SELECT X FROM Y_Procedure(@ProcedureParameters)

此外,我需要从动态SQL过程中选择

Y_Procedure=@Y+'_Procedure'
SELECT X FROM Y_Procedure(@ProcedureParameters)

我还需要将它加载到DataTable: - /

INSERT INTO @Report (CellHorizontal, CellVertical, CellValue) --to TABLE
SELECT Date,X2,X3 FROM Y_GetDisplayData(@Param)

    SET NOCOUNT OFF;

SELECT *
FROM @Report

GetDisplayData作为带参数的select工作,不返回SQL DataTable 并且没有办法重新编码GetDisplayData的程序,这对我来说只是不变的程序。

最后,我需要对此表中的节点进行分组

    INSERT INTO @Report (CellHorizontal, CellVertical, CellValue) 
    SELECT T1.Date, 
IF ((Select grouptype...)=1) T1.X2 + T2.X2
    ELSE IF ((Select grouptype...)=2) AVG(T1.X2,T2.X2), 
IF ((Select grouptype...)=1) T1.X3 + T2.X3
    ELSE IF ((Select grouptype...)=2) AVG(T1.X3,T2.X3), 
    (SELECT T2.Date,X2,X3 FROM Y_GetDisplayData(@Param) T2
    WHERE T2.Date>T1.Date AND T2.Date>=T1.Date)
    FROM Y_GetDisplayData(@Param) T1
    GROUP BY EVERY DAY ???
    --and here is epic fail

我可以在asp服务器上创建所有内容:

C#允许我使用SelectCommand = IzmProc + "_GetDisplayData";

之类的东西

然后我将在ASP.NET服务器上使用DataTables工作(选择特殊数据),但最好在SQL上制作所有内容...但看起来它只是在SQL> _<

上无法实现

我会考虑C#实现

但是我的代码非常奇怪,出错并且不起作用> _<

    public static DataTable GetReport(string Param)
    {
        System.Configuration.ConnectionStringSettings connSetting = ConfigurationManager.ConnectionStrings["FlowServerConnectionString"];
        SqlConnection conn = new SqlConnection(connSetting.ConnectionString);
        SqlCommand cmd = new SqlCommand(Param + " _GetDisplayData 90,1,1,80,1,1,0");
        try
        {
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);

            //------------------------------------------------------------
            int dayOfWeekNumber = (int)DateTime.Today.DayOfWeek - (int)System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
            var previosWeekFirstDay = DateTime.Today.AddDays(-7 - dayOfWeekNumber);
            var yearsterday = DateTime.Today.AddDays(-1);
            var previosWeekLastDay = previosWeekFirstDay.AddDays(6);
            var previosMonthFirstDay = DateTime.Today.AddMonths(-1);
            previosMonthFirstDay = previosMonthFirstDay.AddDays(-previosMonthFirstDay.Day + 1);
            var previosMonthLastDay = previosMonthFirstDay.AddDays(DateTime.DaysInMonth(previosMonthFirstDay.Year, previosMonthFirstDay.Month) - 1);
            //-------------------------------------------------------------
            //Ok... now I need to group it on DT->everyday in month
            DataRow[] drx;
            DataTable newDT= new DataTable();
            foreach (DataRow row in dt.Rows)
            {
                string s = row["DailyRecTime"].ToString();
                drx = dt.Select("DailyRecTime= '" + s + "'");
                for (DateTime x = previosMonthFirstDay; x <= previosMonthLastDay; x.AddDays(1))
                {
                    foreach(DataRow drr in drx)
                    if (x.ToString() == drr[0].ToString())
                    {
                        drx = dt.Select("DailyRecTime= '" + s + "'");
                        DataRow newRow = new DataRow(); //ERROR HERE
                        double[] temp = new double[drx[0].Table.Columns.Count];
                        foreach (DataRow dr in drx)
                        {
                            for(int dc=1; dc<dr.Table.Columns.Count; dc++)
                            {
                                if (dr.Table.Columns[dc].Caption.ToString() == "C1_mol") //for example
                                    temp[dc] += double.Parse(dr[dc].ToString()); // nonsense
                            }
                        }
                        foreach (DataColumn dcl in drx[0].Table.Columns)
                        {
                            newRow.Table.Columns.Add(dcl);
                            newRow[dcl] = temp[dcl]; //error here :)
                        }
                        newDT.Rows.Add(newRow);
                    }
                }
            }

            return newDT;
        }
        catch (Exception)
        {
            return null;
        }

omg ...: - /

感谢您阅读并试图帮助我:3

3 个答案:

答案 0 :(得分:1)

 DataRow newRow = new DataRow(); //ERROR HERE 

DataRow newRow = dt.NewRow();

答案 1 :(得分:1)

DataRow[] foundRows = dt.Select(null,dt.Columns[0].ColumnName); // Sort with Column name
for (int i = 0 ; i <= foundRows.GetUpperBound(0); i++) 
{ object[] arr = new object[foundRows.GetUpperBound(0)];
for (int j = 0; j <= foundRows[i].ItemArray.GetUpperBound(0); j++)
{ arr[j]=foundRows[i][j]; }
DataRow data_row = newDT.NewRow();
data_row.ItemArray=arr;
newDT.Rows.Add(data_row);

}

得到了这个 // Hussain Hyder Ali Khowaja //巴基斯坦卡拉奇

答案 2 :(得分:0)

哦,而不是像这样制作数据视图,并使用它具有的排序功能

dataview v =dt.defaultview;
v.sort="columnName DESC";//columName can be DailyRecTime, DESC fro decending or ASC for asscending
dt=v.toTable();