大家好我的数据库结构如下
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 :(得分:1)
否 - 列每行只能存储一个值。您可以将所有批处理标题合并为一个blob并将其存储为单个值,但是在读取数据时,您必须能够将它们再次分开。
相反 - 看起来好像:
如果这一切都正确,那么你需要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
这应该让你开始。祝你好运。
答案 1 :(得分:0)
为什么不使用Stringbuilder并将所需的行追加到该字符串构建器并将它们写入DB而不是使用字符串。如果需要,分离每列将很难检索数据。因此,声明一个字符串构建器并将行附加到您需要的每一个,然后写入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);