2011-04-06 89 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