private static void DemonstrateMergeTable()
{
DataTable table1 = new DataTable(“Items”);
// Add columns
DataColumn column1 = new DataColumn(“id”, typeof(System.Int32));
DataColumn column2 = new DataColumn(“item”, typeof(System.Int32));
table1.Columns.Add(column1);
table1.Columns.Add(column2);
// Set the primary key column.
table1.PrimaryKey = new DataColumn[] { column1 };
// Add RowChanged event handler for the table.
table1.RowChanged +=
new System.Data.DataRowChangeEventHandler(Row_Changed);
// Add some rows.
DataRow row;
for (int i = 0; i <= 3; i++)
{
row = table1.NewRow();
row["id"] = i;
row["item"] = i;
table1.Rows.Add(row);
}
// Accept changes.
table1.AcceptChanges();
PrintValues(table1, “Original values”);
// Create a second DataTable identical to the first.
DataTable table2 = table1.Clone();
// Add three rows. Note that the id column can’t be the
// same as existing rows in the original table.
row = table2.NewRow();
row["id"] = 14;
row["item"] = 774;
table2.Rows.Add(row);
row = table2.NewRow();
row["id"] = 12;
row["item"] = 555;
table2.Rows.Add(row);
row = table2.NewRow();
row["id"] = 13;
row["item"] = 665;
table2.Rows.Add(row);
// Merge table2 into the table1.
Console.WriteLine(“Merging”);
table1.Merge(table2);
PrintValues(table1, “Merged With table1″);
}
private static void Row_Changed(object sender,
DataRowChangeEventArgs e)
{
Console.WriteLine(“Row changed {0}\t{1}”,
e.Action, e.Row.ItemArray[0]);
}
private static void PrintValues(DataTable table, string label)
{
// Display the values in the supplied DataTable:
Console.WriteLine(label);
foreach (DataRow row in table.Rows)
{
foreach (DataColumn col in table.Columns)
{
Console.Write(“\t ” + row[col].ToString());
}
Console.WriteLine();
}
}
我是这样使用的,请看:
void bind()
{
BLL.Ts_Planning bll = new BLL.Ts_Planning();
Capsugel.BLL.Users bll2 = new BLL.Users();
DataTable dt = bll2.GetTsList().Tables[0];
DataSet ds = new DataSet();
DataTable dt2;
dt2 = bll.GetListByUid_All_Planning_Count(dt.Rows[0]["user id"].ToString(), TextBox2.Text.ToString(), TextBox3.Text.ToString()).Tables[0];
for (int i = 1; i < dt.Rows.Count – 1; i++)
{
dt2.Merge(bll.GetListByUid_All_Planning_Count(dt.Rows[i]["user id"].ToString(), TextBox2.Text.ToString(), TextBox3.Text.ToString()).Tables[0]);
}
GridView1.DataSource = dt2;
GridView1.DataBind();
}
下面是存储过程,有没有高人来指点一下,虽然问题已解决,谢谢DataTable的Merge :-),但是好像很烦:
ALTER PROCEDURE [dbo].[Ts_Planning_Count]
@Uid varchar(20),
@date1 smalldatetime,
@date2 smalldatetime
AS
Select U.Name,AAAAA.* From
[Users] U,
(Select * From
(Select count(Pid) as [Explor/Penetra] from Ts_Planning
where
Purpose = ‘Explor/Penetra’ and UID=@Uid and (Date between @date1 and @date2)) A,
(Select count(Pid) as [Relation buliding] from Ts_Planning
where
Purpose = ‘Relation buliding’ and UID=@Uid and (Date between @date1 and @date2)) B,
(Select count(Pid) as [Prevent service] from Ts_Planning
where
Purpose = ‘Prevent service’ and UID=@Uid and (Date between @date1 and @date2)) C,
(Select count(Pid) as [Complaint handing] from Ts_Planning
where
Purpose = ‘Complaint handing’ and UID=@Uid and (Date between @date1 and @date2)) D,
(Select count(Pid) as [Reactive service] from Ts_Planning
where
Purpose = ‘Reactive service’ and UID=@Uid and (Date between @date1 and @date2)) E,
(Select count(Pid) as [Trial run] from Ts_Planning
where
Purpose = ‘Trial run’ and UID=@Uid and (Date between @date1 and @date2)) F,
(Select count(Pid) as [Key] from Ts_Planning
where
Class = ‘Key’ and UID=@Uid and (Date between @date1 and @date2)) AA,
(Select count(Pid) as [Core] from Ts_Planning
where
Class = ‘Core’ and UID=@Uid and (Date between @date1 and @date2)) BB,
(Select count(Pid) as [Target] from Ts_Planning
where
Class = ‘Target’ and UID=@Uid and (Date between @date1 and @date2)) CC,
(Select count(Pid) as [Potential] from Ts_Planning
where
Class = ‘Potential’ and UID=@Uid and (Date between @date1 and @date2)) DD,
(Select count(Pid) as [WM] from Ts_Planning
where
Segment = ‘WM’ and UID=@Uid and (Date between @date1 and @date2)) AAA,
(Select count(Pid) as [TCM] from Ts_Planning
where
Segment = ‘TCM’ and UID=@Uid and (Date between @date1 and @date2)) BBB,
(Select count(Pid) as [DS] from Ts_Planning
where
Segment = ‘DS’ and UID=@Uid and (Date between @date1 and @date2)) CCC,
(Select count(Pid) as Visit from Ts_Planning
where
Activity = ‘Visit’ and UID=@Uid and (Date between @date1 and @date2)) AAAA,
(Select count(Pid) as Travel from Ts_Planning
where
Activity = ‘Travel’ and UID=@Uid and (Date between @date1 and @date2)) BBBB,
(Select count(Pid) as [O/HO] from Ts_Planning
where
Activity = ‘O/HO’ and UID=@Uid and (Date between @date1 and @date2)) CCCC,
(Select count(Pid) as [Meeting/Training] from Ts_Planning
where
Activity = ‘Meeting/Training’ and UID=@Uid and (Date between @date1 and @date2)) DDDD,
(Select count(Pid) as [CFM&Expo] from Ts_Planning
where
Activity = ‘CFM&Expo’ and UID=@Uid and (Date between @date1 and @date2)) EEEE,
(Select count(Pid) as [H&V] from Ts_Planning
where
Activity = ‘H&V’ and UID=@Uid and (Date between @date1 and @date2)) FFFF,
(Select SUM(ActivityTimes) as [ActivityTimes] from Ts_Planning
where
UID=@Uid and (Date between @date1 and @date2)) AAAAA
) AAAAA
where U.Uid = @Uid
最新评论