简体   繁体   English

如何使用一个SQL查询创建多个DataTable?

[英]How to create multiple DataTables with one sql query?

I'm creating an application that loads data from SQL Database once a day and saves it into a text file. 我正在创建一个应用程序,该应用程序每天一次从SQL数据库加载数据并将其保存到文本文件中。 The main table is a "Transactions" table, which holds data about all transactions made on that day. 主表是一个“交易”表,其中包含有关当天进行的所有交易的数据。 One of the columns represents a middle-man call sign. 列之一代表中间人呼号。 My program saves the data in a DataTable first and then with a StringBuilder I give it the proper form and finally save it into a text file with StreamWriter. 我的程序首先将数据保存在DataTable中,然后使用StringBuilder将其赋予正确的格式,最后使用StreamWriter将其保存到文本文件中。

My question is, how or on which stage of the process can I distinguish one table entry from another. 我的问题是,如何或在哪个阶段区分一个表条目和另一个表条目。 I want to create two files: one with transactions made by middle-man A and B. This is my code so far: 我想创建两个文件:一个文件是由中间人A和B进行的事务。这是到目前为止的代码:

// Query for Data

row = new SqlDataAdapter("SELECT [MSISDN], [Amount], [Transaction_ID], POS.[Name], MNO.[Call Sign] FROM"
                     + "[Transactions] join [POS] "
                     + "on Transactions.POS_ID = POS.idPOS "
                     + "join [MNO] on Transactions.MNO_ID = MNO.idMNO "
                     + "where [Status] = '1'", con);
row.Fill(Row);

// Save Data in StringBuilder

for (int i = 0; i < Row.Rows.Count; i++)
{
      sb.Append(Row.Rows[i].ItemArray[0].ToString()).Append(",");
      double amount = Convert.ToDouble(Row.Rows[i].ItemArray[1].ToString());
      sb.Append(Math.Round(amount, 2).ToString().Replace(",", ".")).Append(",");
      sb.Append(Row.Rows[i].ItemArray[2].ToString()).Append(",");
      sb.Append(Row.Rows[i].ItemArray[3].ToString()).Append(",");
    sb.Append(Row.Rows[i].ItemArray[4].ToString()).Append(",").Append(Environment.NewLine);
}


// Create a file from StringBuilder
mydocpath = @"C:\Transactions\" + fileDate.ToString(format) + ".txt";
FileStream fsOverwrite = new FileStream(mydocpath, FileMode.Create);
using (StreamWriter outfile = new StreamWriter(fsOverwrite))
{
       outfile.WriteAsync(sb.ToString());
}

Hope I was clear enough. 希望我足够清楚。 English isn't my strong side. 英语不是我的强项。 As well as coding for what it seems... 以及对看起来的东西进行编码...

One option. 一种选择。

Put all your data into a DataSet. 将所有数据放入数据集。 And then do Xsl transformations against the ds.GetXml(). 然后对ds.GetXml()进行Xsl转换。 Here is kind of an example: 这是一个示例:

http://granadacoder.wordpress.com/2007/05/15/xml-to-xml-conversion/ http://granadacoder.wordpress.com/2007/05/15/xml-to-xml-conversion/

But what I would do is eliminate the DataTable altogether. 但是我要做的是完全消除DataTable。 Use an IDataReader. 使用IDataReader。 Loop over the data. 遍历数据。 Maybe do the original query as "Order By Middle-Man-Identifer", and then when the middleManIdentifer "makes a jump", close the previous file and write a new one. 也许将原始查询作为“按中间人标识符排序”,然后当MiddleManIdentifer“进行跳转”时,关闭前一个文件并编写一个新文件。 Something like that. 这样的事情。

You may be able to learn something from this demo: 您可能可以从此演示中学到一些东西:

http://granadacoder.wordpress.com/2009/01/27/bulk-insert-example-using-an-idatareader-to-strong-dataset-to-sql-server-xml/ http://granadacoder.wordpress.com/2009/01/27/bulk-insert-example-using-an-idatareader-to-strong-dataset-to-sql-server-xml/

Here is a couple of IDataReader helpers: 这是几个IDataReader助手:

http://kalit-codesnippetsofnettechnology.blogspot.com/2009/05/write-textfile-from-sqldatareader.html http://kalit-codesnippetsofnettechnology.blogspot.com/2009/05/write-textfile-from-sqldatareader.html

and

How to efficiently write to file from SQL datareader in c#? 如何在C#中从SQL数据读取器有效地写入文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM