使用DataTable问题填充DataSet? [英] Fill a DataSet with DataTable questions?
问题描述
你好,
我是.net的新手,我正在尝试建立一个报告应用程序,
根据id查询4个不同的表,并且我需要将它们放在
相同的桌子上以便于查看。
基本上,我有一个查询可以抓取我需要的所有ID对于
其他4个查询,但我不知道如何将它们放入DataTable或
DataSet,或者如果这是最好的方法。单独查询
所有工作都没有问题。
4个报告查询对字段进行计数并返回结果
$ b其他标准为$ b,其中一个是首次收集的ID。
现在我让它工作,他们可以选择要查看的ID,并且
然后我将它传递给4个查询,这些查询都相应地将结果返回到他们的
自己的数据网格中。我的问题是这可以在我可以的地方完成
有一个表根据id显示所有结果。
提前谢谢,
Kevyn
Hello,
I am new to .net and am trying to build a report application that
queries 4 different tables based on a id, and I need to return them in
the same table for easy viewing.
Basically, I have one querie that grabs all of the id''s I need for the
other 4 queries, but I am not sure how to get them into a DataTable or
DataSet, or if that is the best way to do this. Seperately the queries
all work with no problems.
The 4 reporting queries do a count on a field and return a result based
on other criteria, one of which is the id which is first gathered.
Right now I have it working where they can select the ID to view, and
then I pass it to the 4 queries which all return the results in their
own datagrid accordingly. My question is can this be done where I can
have one table which shows all the results based on the id.
Thanks in advance,
Kevyn
推荐答案
但是,您需要运行四个单独的查询,因为查询
返回一个表,而不管查询的复杂程度如何。由于
数据表是addititve,您可以触发四个单独的查询并将
结果添加到数据集。
- < br $>
________________________
热烈的问候,
Alvin Bruney [MVP ASP.NET]
[无耻作者插件]
OWC使用.NET的黑皮书
www.lulu.com/owc ,亚马逊
专业版VSTO.NET - Wrox / Wiley 2006
------------------------- ------------------------------
< da ***** *@gmail.com>在消息中写道
news:11 ********************* @ i39g2000cwa.googlegro ups.com ...
Sure, however, you will need to run four separate queries because a query
returns one table irrespective of the complexity of the query. Since
datatables are addititve, you can fire four separate queries and add the
results to a dataset.
--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]
[Shameless Author plug]
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Professional VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------
<da******@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
你好,
我是.net的新手,我正在尝试构建一个报表应用程序,它根据id查询4个不同的表,我需要在
同一张桌子,方便查看。
基本上,我有一个查询,可以抓取我需要的其他4个查询的所有ID,但我不知道如何将它们放入DataTable或
DataSet中,或者如果这是最好的方法。单独查询
所有工作都没有问题。
4个报告查询对字段进行计数并返回基于其他条件的结果
,其中一个是id这是第一次收集。
现在我让它工作,他们可以选择要查看的ID,然后我将它传递给4个查询,这些查询都返回结果相应地拥有datagrid。我的问题是可以做到这一点,我可以在一张桌子上显示基于id的所有结果。
提前致谢,
Kevyn
Hello,
I am new to .net and am trying to build a report application that
queries 4 different tables based on a id, and I need to return them in
the same table for easy viewing.
Basically, I have one querie that grabs all of the id''s I need for the
other 4 queries, but I am not sure how to get them into a DataTable or
DataSet, or if that is the best way to do this. Seperately the queries
all work with no problems.
The 4 reporting queries do a count on a field and return a result based
on other criteria, one of which is the id which is first gathered.
Right now I have it working where they can select the ID to view, and
then I pass it to the 4 queries which all return the results in their
own datagrid accordingly. My question is can this be done where I can
have one table which shows all the results based on the id.
Thanks in advance,
Kevyn
感谢您的回复。
有没有一个例子可以告诉我你取得一个结果
查询,主要是id,然后将其作为变量传递给其他
查询,然后将它们全部放入数据集中?
大约有40个左右的id'需要传递并返回
结果,所以我可以将它们放在数据集中进行显示。
>
提前致谢!
Kevyn
Thanks for the response.
Is there an example you can show me where you take the results of one
query, mainly the id, and then pass that as a variable to the other
queries and then put them all in a dataset?
There are about 40 or so id''s which will need to pass and return the
results, so then I can put them in a dataset for displaying.
Thanks in advance!
Kevyn
基本上......这是我的代码..你可以协助合并吗?
我正在为每个查询创建一个数据集,唯一的常见字段是
track_id,我需要对结果进行分组by each track_id。
protected void Page_Load(对象发送者,Eve ntArgs e)
{
string dataBaseName;
string startDate;
string endDate;
dataBaseName =(string)Session [" repositoryID"];
startDate =(string)Session [" startDate"];
endDate =(string)Session [" endDate"];
dataBaseName = dataBaseName.Trim();
// create&配置与数据库的连接
string strConnString =
ConfigurationManager.ConnectionStrings ["" + dataBaseName +
""]。ConnectionString;
SqlConnection theConnection = new SqlConnection(strConnString);
//为点击创建数据集
DataSet dsClicks = new DataSet();
DataTable clickTable = new DataTable(" clicks");
//创建列
DataColumn trackID = clickTable.Columns.Add(" track_id",
typeof(string));
DataColumn uniqueClicks =
clickTable.Columns.Add(" unique_clicks",typeof(string));
DataColumn totalClicks =
clickTable.Columns.Add(" total_clicks",typeof(string));
//设置主键
clickTable.PrimaryKey = new DataColumn [] {trackID};
//添加表格
dsClicks.Tables.Add(clickTable);
//创建命令和适配器
SqlDataAdapter clickAdapter = new SqlDataAdapter();
SqlCommand clickCommand =新的SqlCommand(
" SELECT track_id,count(distinct ip)as unique_clicks,
count(ip)as total_clicks"
+" FROM tracking(NOLOCK)"
+" WHERE event_type =''C''"
+" AND record_created> =''" + startDate +"''"
+" AND record_created< =''" + endDate +"''"
+" GROUP BY track_id"
+" ORDER BY track_id",the Connectction);
clickAdapter.SelectCommand = clickCommand;
//填充dataTable
clickAdapter.Fill(dsClicks," unique_clicks");
//为潜在客户创建数据集
DataSet dsLeads = new DataSet();
DataTable leadsTable = new DataTable(" leads");
//创建列
DataColumn trackIDLead = leadsTable.Columns.Add(" track_id",
typeof (字符串));
DataColumn leads = leadsTable.Columns.Add(" leads",
typeof(string));
//设置列属性
//设置主键
leadsTable.PrimaryKey = new DataColumn [] {trackIDLead};
//添加表格
dsLeads.Tables.Add(leadsTable);
//创建命令和适配器
SqlDat aAdapter leadAdapter = new SqlDataAdapter();
SqlCommand leadsCommand = new SqlCommand(
" SELECT track_id,count(distinct ip)as leads"
+" FROM tracking(NOLOCK)"
+" WHERE event_type =''L''"
+" AND record_created> =' " + startDate +"''"
+" AND record_created< =''" + endDate +"''"
+" GROUP BY track_id"
+" ORDER BY track_id",the Connectction);
leadsAdapter.SelectCommand = leadsCommand;
//填充dataTable
leadsAdapter.Fill(dsLeads," leads");
}
Basically..Here is my code...can you assist with merging?
I am creating a dataset for each query, the only common field is
track_id, which I need to group the results by for each track_id.
protected void Page_Load(object sender, EventArgs e)
{
string dataBaseName;
string startDate;
string endDate;
dataBaseName = (string) Session["repositoryID"];
startDate = (string) Session["startDate"];
endDate = (string) Session["endDate"];
dataBaseName = dataBaseName.Trim();
// create & configure the connection to database
string strConnString =
ConfigurationManager.ConnectionStrings["" + dataBaseName +
""].ConnectionString;
SqlConnection theConnection = new SqlConnection(strConnString);
//create dataSet for Clicks
DataSet dsClicks = new DataSet();
DataTable clickTable = new DataTable("clicks");
//create the column(s)
DataColumn trackID = clickTable.Columns.Add("track_id",
typeof(string));
DataColumn uniqueClicks =
clickTable.Columns.Add("unique_clicks", typeof(string));
DataColumn totalClicks =
clickTable.Columns.Add("total_clicks", typeof(string));
//set the primary key
clickTable.PrimaryKey = new DataColumn[] { trackID };
//add the table
dsClicks.Tables.Add(clickTable);
//create the command and adapter
SqlDataAdapter clickAdapter = new SqlDataAdapter();
SqlCommand clickCommand = new SqlCommand(
" SELECT track_id, count(distinct ip) as unique_clicks,
count(ip) as total_clicks "
+ "FROM tracking (NOLOCK) "
+ "WHERE event_type = ''C'' "
+ "AND record_created >= ''" + startDate + "'' "
+ "AND record_created <= ''" + endDate + "'' "
+ "GROUP BY track_id "
+ "ORDER BY track_id", theConnection);
clickAdapter.SelectCommand = clickCommand;
//fill the dataTable
clickAdapter.Fill(dsClicks, "unique_clicks");
//create dataSet for Leads
DataSet dsLeads = new DataSet();
DataTable leadsTable = new DataTable("leads");
//create the column(s)
DataColumn trackIDLead = leadsTable.Columns.Add("track_id",
typeof(string));
DataColumn leads = leadsTable.Columns.Add("leads",
typeof(string));
//set column(s) properties
//set the primary key
leadsTable.PrimaryKey = new DataColumn[] {trackIDLead};
//add the table
dsLeads.Tables.Add(leadsTable);
//create the command and adapter
SqlDataAdapter leadsAdapter = new SqlDataAdapter();
SqlCommand leadsCommand = new SqlCommand(
"SELECT track_id, count(distinct ip) as leads "
+ "FROM tracking (NOLOCK) "
+ "WHERE event_type = ''L'' "
+ "AND record_created >= ''" + startDate + "'' "
+ "AND record_created <= ''" + endDate + "'' "
+ "GROUP BY track_id "
+ "ORDER BY track_id", theConnection);
leadsAdapter.SelectCommand = leadsCommand;
//fill the dataTable
leadsAdapter.Fill(dsLeads, "leads");
}
这篇关于使用DataTable问题填充DataSet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!