使用streamreader读取当前行时,请检查下一行

本文关键字:检查 一行 读取 streamreader 使用 | 更新日期: 2023-09-27 18:21:39

这是我的文件的内容:


atul,salunke
amol,salunke
anil,salunke
;dandegaon
amar,salunke
;bhoom
akash,salunke
;chennai
pappu,salunke

这是我的密码。在计数5之后刷新批量时,我缺少注释;chennai。这就是为什么我在阅读当前行时需要检查下一行的内容。

这是我的代码:

int counter = 0;
int cnt = 0;
string line = "";
SqlConnection con = new SqlConnection("Data Source=atul-pc;Initial Catalog=TestDB;Integrated Security=True");
SqlBulkCopy bulkCopy = new SqlBulkCopy(con, SqlBulkCopyOptions.TableLock
                                       | SqlBulkCopyOptions.FireTriggers
                                       | SqlBulkCopyOptions.UseInternalTransaction, null);
con.Open();
bulkCopy.BatchSize = 5;
bulkCopy.BulkCopyTimeout = 3000;
string[] sField = null;
DataRow row;
System.IO.StreamReader file = new System.IO.StreamReader("H:''sample file.txt");
DataTable table = new DataTable();
DataColumn col1 = new DataColumn("iID"); table.Columns.Add("iID", System.Type.GetType("System.Int32"));
DataColumn col2 = new DataColumn("vchFirstName"); table.Columns.Add("vchFirstName", System.Type.GetType("System.String"));
DataColumn col3 = new DataColumn("vchLastName"); table.Columns.Add("vchLastName", System.Type.GetType("System.String"));
DataColumn col4 = new DataColumn("Comment"); table.Columns.Add("Comment", System.Type.GetType("System.String"));
bulkCopy.DestinationTableName = "userDetails";
bulkCopy.ColumnMappings.Add("iID", "iID");
bulkCopy.ColumnMappings.Add("vchFirstName", "vchFirstName");
bulkCopy.ColumnMappings.Add("vchLastName", "vchLastName");
bulkCopy.ColumnMappings.Add("Comment", "Comment");
int priv = 0;
 while ((line = file.ReadLine()) != null)
{
     int columnCount =0 ;
    char[] cDelim = new char[] { ',' };
    sField = line.Split(cDelim);
    if (line.Contains(","))
    {
        counter++;
        priv = counter;
        row = table.NewRow();
        row["iID"] = counter;
        foreach (string s in sField)
        {
            columnCount++;
            row[columnCount] = s.Trim();
        }
        table.Rows.Add(row);
    }
    if (line.Contains(";"))
    {
        DataRow[] result = table.Select("iID="+priv+"");
        result[0]["Comment"] = line.Trim();
    }
    if (Math.Floor((double)counter / 5) != Math.Floor((double)(counter - 1) / 5))
    {
        bulkCopy.WriteToServer(table);// here i missing coment  ;chennai
        table.Clear();
    }
    cnt++;
}

使用streamreader读取当前行时,请检查下一行

我很难跟踪您,但我认为问题是您在bulkCopy.WriteToServer(table)之后调用table.Clear()。您应该致电table.Rows.Clear()。调用Table.Clear()会转储表中的所有内容,包括列。看起来你的意思是只转储行。

当你在做的时候,你也可以做一些改变来帮助你。。。

  • 使用File.ReadAllLines()
  • 使用ForEach循环
  • 在选择琴弦时修剪琴弦
  • 使用string.Format()

像这样的东西:

int priv = 0;
var lines = File.ReadAllLines("H:''sample file.txt").Select(s=>s.Trim());
foreach (var line in lines)
{
    if (line.Contains(";"))
    {
        DataRow[] result = table.Select(string.Format("iID={0}", priv));
        result[0]["Comment"] = line;
    }
    else
    {
        row = table.NewRow();
        row["iID"] = counter;
        counter++;
        priv = counter;
        var words = line
            .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
            .Select(it => it.Trim());
        int columnCount = 0;
        foreach (var word in words)
        {
            columnCount++;
            row[columnCount] = word;
        }
        table.Rows.Add(row);
    }
    if (Math.Floor((double)counter / 5) != Math.Floor((double)(counter - 1) / 5))
    {
        bulkCopy.WriteToServer(table);// here i missing coment  ;chennai
        table.Clear();
    }
    cnt++;
}