从SQL Server DB导出PDF并将Map写入文本文件

我需要编写一个应用程序或Query来将大量PDF文件导出到文件中,并创建一个分隔文本文件,该文件显示文件的位置并包含记录的ID。

我正在考虑做的是使用一个控制台应用程序,它会在从数据库导出PDF后将条目写入文本文件,这样我在编写文本文件时就可以将所有信息放在一起,这样我就可以确保所有分隔文本文件中的数据是准确的。

起初我正在考虑使用数据集来执行此操作,但是将有超过50,000行数据。 我不太确定DataTable会更好

我也在考虑使用BCP实用程序,但是从我正在阅读的内容中,导出并没有从Data中返回PDF文件,这是真的吗?

我可能会认为自己是初学者,编程就像这样。 我应该如何使用这样的数据结构? 我会使用光标,如果是这样,我将如何设置它以适应我正在做的事情?

更新

我将尝试使用DataSet选项,但是使用do while循环一次将其限制为1天的数据,这样我就可以从数据的开头每天循环到今天的日期。 所以我将做一天的数据,然后摆脱DataSet,然后做下一个日期。

有没有人在我的逻辑中看到会导致问题的任何内容?

当我最终完成对问题的所有不同方法的研究时,编码真的非常简单。 我根本没有使用BCP。

我创建了变量,以获取我在文本文件中提取的信息。

  1. 文件名
  2. 日期(从原始创建日期的SQL表)
  3. 案例编号(要链接到的第三方程序的内部标识符)
  4. 描述(取自SQL表来描述文档)

然后我将应用程序用于将代码一次编写为PDF

using (SqlConnection Conn = new SqlConnection(strSQLConn)) { //open the connection Conn.Open(); Console.WriteLine("the connection is open"); //Variables needed for looping DateTime Today = System.DateTime.Now; DateTime StartDate = Convert.ToDateTime("2008-06-11 00:00:00"); //DateTime StartDate = Today.AddDays(-10); Console.WriteLine("Converting the Documents from " + StartDate.ToString() + " - TO - " + Today.ToString()); Console.WriteLine("Press Any Key to continue."); Console.ReadLine(); int RecordCount = 0; ulong ByteCount = 0; int i = 1; foreach (DateTime day in EachDay(StartDate, Today)) { String strDay = day.ToString(); // Create a SQLCommand to retrieve Data SqlCommand getRecords = new SqlCommand("spRecapturePDF", Conn); getRecords.CommandType = CommandType.StoredProcedure; getRecords.Parameters.Add(new SqlParameter("@OneDay", strDay)); SqlDataReader reader = getRecords.ExecuteReader(); //stuff exporting the binary code to the PDF format FileStream fs; BinaryWriter bw; int buffersize = 100; byte[] outbyte = new byte[buffersize]; long retval; long startIndex = 0; int j = 1; while (reader.Read()) { strFileName = reader.GetString(0) + "-" + i + "-" + j; strDock_no = reader.GetString(0); dtFiledate = reader.GetDateTime(2); strDescription = reader.GetString(4); fs = new FileStream("c:\\FolderName\\" + strFileName + ".pdf", FileMode.OpenOrCreate, FileAccess.Write); bw = new BinaryWriter(fs); startIndex = 0; retval = reader.GetBytes(1,startIndex,outbyte,0,buffersize); while (retval == buffersize) { bw.Write(outbyte); bw.Flush(); startIndex += buffersize; retval = reader.GetBytes(1,startIndex,outbyte,0,buffersize); } //write the remaining buffer. bw.Write(outbyte,0,(int)retval); ByteCount = ByteCount + Convert.ToUInt64(fs.Length); bw.Flush(); //close the output file bw.Close(); fs.Close(); //need to write to the Text file here. TextWriter tw = new StreamWriter(path,true); tw.WriteLine(strDock_no + "~" + dtFiledate.ToString() + "~" + "c:\\FolderName\\" + strFileName + ".pdf" + "~" + strDescription); tw.Close(); // increment the J variable for the Next FileName j++; RecordCount++; } //close the reader and the connection reader.Close(); i++; } Console.WriteLine("Number of Records Processed: " + RecordCount.ToString()); Console.WriteLine("for a Total of : " + ByteCount + " Bytes"); Decimal MByteCount = new Decimal(2); MByteCount = Convert.ToDecimal(ByteCount) / 1024 / 1024; Decimal GByteCount = new Decimal(2); GByteCount = MByteCount / 1024; Console.WriteLine("Total MBs : " + MByteCount.ToString() + " MB"); Console.WriteLine("Total GBs : " + GByteCount.ToString() + " GB"); Console.WriteLine("Press Enter to Continue ..."); Console.ReadLine(); } 

本规范附在一份日期声明中,该声明日期从开始日期到结束日期。 在该foreach语句中,Application调用了一个存储过程,该存储过程在指定的日期内被调用,以调用当天输入的记录。

i创建了变量ij ,因为即使我有相同的案例编号,我也需要一个唯一的文件名。 i代表了这一天(因为我在我的select语句中每天都去了), j代表select语句中当天的记录号。

foreachwhile循环被封装在一个using(conn)这样无论连接最终会被关闭。

在while循环结束时,我写了文本文件。 Text文件是在所有循环之外创建的,这样我就可以附加文件而不是覆盖它。 那段代码是:

 string path = @"c:\\FolderName\\TextFile.txt"; if (!File.Exists(path)) { TextWriter tw = new StreamWriter(path, false); tw.WriteLine("Dock_No~Date~FileName(Location)~Description"); tw.Close(); } 

我希望这有助于其他人。 我遗漏了所有与我正在寻找的function无关的Console.WritelineConsole.ReadLine代码。 我添加了一些代码,它们会计算写入的字节数和一些代码来计算处理的记录。 这只是有趣的事情要知道,我需要在最后清理有趣的东西。

这些是从SQL Server中的Blob字段完成大量PDF文件提取所需的内容,减去一些连接Mumbo Jumbo

Foreach Day设置

这是我用来使foreach以我想要的方式工作的代码。

 static public IEnumerable EachDay(DateTime Startdate, DateTime EndDate) { for (var day = Startdate.Date; day.Date <= EndDate.Date; day = day.AddDays(1)) yield return day; } 
    Interesting Posts