2011-04-06 39 views
0

大家好,我有我的数据库结构如下是否有可能到多个数据保存到数据库列

Field  Type 
FileHeader longblob 
BatchHeader longblob 
Entry   longblob 
BtchEntry  longblob 
FileControl longblob 

我将要插入的数据如下

101 111111111 1111111111104021031A094101              
52201    1     1   PPD1   110402110402 1111000020000001 
6221110000251    00000000011    1      1 0111000020000001 
822000000100111000020000000000000000000000011         111000020000001 
52251    1     1   CCD1   110402110402 1111000020000002 
6281110000251    00000000011    1      1 0111000020000002 
822500000100111000020000000000010000000000001         111000020000002 
9000006000001000000060066600012000000000003000000000003          

你可以观察到有多行以5,6和8开始。我想将这些单独保存到我的表的相应列中。如果可以的话,是否可以提到最好的方法来做到这一点。如果不清楚请注明

我编写的代码是

using (StreamReader srRead = new StreamReader(filePath)) 
    { 
     while (srRead.Peek() >= 0) 
     { 
      strLine = srRead.ReadLine(); 
      if (strLine.StartsWith("1")) 
      { 
       strFileHeader = strLine; 
      } 
      if (strLine.StartsWith("5")) 
      { 
       strBatchHeader = strLine; 
      } 
      if (strLine.StartsWith("6")) 
      { 
       strEntry = strLine; 
      } 
      if (strLine.StartsWith("8")) 
      { 
       strBtchcntrl = strLine; 
      } 
      if (strLine.StartsWith("9")) 
      { 
       strFileCntrl = strLine; 
      } 
     } 

    string strQuery = "insert into tblfiles(FName, FData,FileHeader,BatchHeader,Entry,BtchEntry,FileControl) values (@_FName,@_FData,@_FileHeader,@_BtchHeader,@_EntryDets,@_BtchCntrl,@_FileCntrl)"; 
     MySqlCommand cmd = new MySqlCommand(strQuery); 
     cmd.Parameters.Add("@_FName", MySqlDbType.VarChar).Value = filename; 
     cmd.Parameters.Add("@_FData", MySqlDbType.LongBlob).Value = bytes; 
     cmd.Parameters.Add("@_FileHeader", MySqlDbType.LongBlob).Value = strFileHeader; 
     cmd.Parameters.Add("@_BtchHeader", MySqlDbType.LongBlob).Value = strBatchHeader; 
     cmd.Parameters.Add("@_EntryDets", MySqlDbType.LongBlob).Value = strEntry; 
     cmd.Parameters.Add("@_BtchCntrl", MySqlDbType.LongBlob).Value = strBtchcntrl; 
     cmd.Parameters.Add("@_FileCntrl", MySqlDbType.LongBlob).Value = strFileCntrl; 
     InsertUpdateData(cmd); 

但是,这将插入最新的数据库,但我想挽救每一条线路按我说

+0

您已将此标记为“家庭作业”。你能否提供更多的作业细节(你正试图解决的问题)以及你现在拥有的**代码是否无效。我们不能指望你为你做功课。 – ChrisF 2011-04-06 11:35:51

+0

@Chris:首先,我只想知道如何将多个数据插入到我的数据库中的指定列 – Dotnet 2011-04-06 11:36:59

+0

为什么不直接通过结合参数化查询解析文件,您可以设置值列。 – Bobby 2011-04-06 11:40:09

回答

1

否 - 列只能存储每行一个值。您可以将所有批处理标题合并到一个Blob中,并将其作为单个值存储,但是当您读取数据时,您必须能够再次将它们分开。

相反 - 它看起来好像:

  1. 每个文件与“1”的记录开始和以“9”记录结束
  2. 每个文件都包含零点或多个批次
  3. 每批开始与“5”的记录,并用“8”记录结束
  4. 每批包含零个或多个条目(“6”的记录)

如果是所有正确的,那么你就需要3个表,将是这个样子:

文件表:

Field   Type 
----------- -------- 
FileID  integer # unique file ID - see AUTO_INCREMENT in the MySQL reference 
FName   varchar 
FData   longblob 
FileHeader longblob # '1' record 
FileControl longblob # '9' record 

批表:

Field   Type 
----------- -------- 
FileID  integer # references a row in the File table 
BatchID  integer # unique batch ID 
BatchHeader longblob # '5' record 
BatchControl longblob # '8' record 

BatchEntry表:

Field   Type 
----------- -------- 
BatchID  integer # references a row in the Batch table 
EntryId  integer # unique file ID 
Entry   longblob # '6' record 

这应该让你开始了。祝你好运。

0

为什么不您可以使用Stringbuilder并将所需的行追加到该字符串构建器,并将它们写入数据库而不是使用字符串。如果需要,分隔每列将是一个难以检索数据的难题。因此声明的字符串生成器,并追加行的每一个你需要的毕竟写入DB

string strFileHeader = string.Empty; 
StringBuilder strBatchHeader=new StringBuilder(); 
StringBuilder strEntry=new StringBuilder(); 
StringBuilder strBtchcntrl=new StringBuilder(); 
string strFileCntrl = string.Empty; 


using (StreamReader srRead = new StreamReader(filePath)) 
    { 
     while (srRead.Peek() >= 0) 
     { 
      strLine = srRead.ReadLine(); 
      if (strLine.StartsWith("1")) 
      { 
       strFileHeader = strLine; 
      } 
      if (strLine.StartsWith("5")) 
      { 

       strBatchHeader.AppendLine(strLine); 
      } 
      if (strLine.StartsWith("6")) 
      { 
       strEntry.AppendLine(strLine); 
      } 
      if (strLine.StartsWith("8")) 
      { 
       strBtchcntrl.AppendLine(strLine); 
      } 
      if (strLine.StartsWith("9")) 
      { 
       strFileCntrl = strLine; 
      } 
     } 

string strQuery = "insert into tblfiles(FName, FData,FileHeader,BatchHeader,Entry,BtchEntry,FileControl) values (@_FName,@_FData,@_FileHeader,@_BtchHeader,@_EntryDets,@_BtchCntrl,@_FileCntrl)"; 
     MySqlCommand cmd = new MySqlCommand(strQuery); 
     cmd.Parameters.Add("@_FName", MySqlDbType.VarChar).Value = filename; 
     cmd.Parameters.Add("@_FData", MySqlDbType.LongBlob).Value = bytes; 
     cmd.Parameters.Add("@_FileHeader", MySqlDbType.LongBlob).Value = strFileHeader; 
     cmd.Parameters.Add("@_BtchHeader", MySqlDbType.LongBlob).Value = strBatchHeader.ToString(); 
     cmd.Parameters.Add("@_EntryDets", MySqlDbType.LongBlob).Value = strEntry.ToString(); 
     cmd.Parameters.Add("@_BtchCntrl", MySqlDbType.LongBlob).Value = strBtchcntrl.ToString(); 
     cmd.Parameters.Add("@_FileCntrl", MySqlDbType.LongBlob).Value = strFileCntrl; 
     InsertUpdateData(cmd); 
+0

好主意,但我想知道替代方案 – Dotnet 2011-04-06 12:57:08