2015-05-19 28 views
1

我尝试比较下一个位置fecmov当前位置,但没有工作,他们总是不同的,虽然他们不是验证比较两行用C#OleDbDataReader擅长

我有这样的代码:

string FECMOV; 
string filePath = Path.GetDirectoryName(FileUploader.PostedFile.FileName); 
     if (Path.GetExtension(filePath) == ".xls") { 
        con1 = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text;MaxScanRows=0\"", filePath); 
       } 
       else if (Path.GetExtension(filePath) == ".xlsx") { 
        con1 = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=Yes;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text;MaxScanRows=0\"", filePath); 
       } 

       using (OleDbConnection connect = new System.Data.OleDb.OleDbConnection(con1)) { 

        connect.Open(); 

        OleDbCommand command = new System.Data.OleDb.OleDbCommand("select * from [Sheet1$]", connect); 

        using (OleDbDataReader dr = command.ExecuteReader()) { 
         if (dr.HasRows) { 
          while (dr.Read()) { 

           if (dr.IsDBNull(4)) { 
            FECMOV = ""; 
           } 

           else { 
            FECMOV = dr[4].ToString().Trim(); 
           } 
           //this validation I try to compare 
           // the current position with the next     
           //position fecmov, but not working, 


             if (FECMOV[a] != FECMOV[a + 1]) 
             { 
              a=a+1 
             } 
        //They are always different though they are not validate 
            }}} 
+1

FECMOV是一个字符串,您所在的位置'了'与字符的位置'A + 1'比较字符。你期望你的字符串一直由同一个字符组成吗? – Steve

+0

是的,我需要比较第一个寄存器与同一列的第二个寄存器... – YotiS

回答

0

试试这个:

public class DataRowComparer : IEqualityComparer<DataRow> 
{ 
    public bool Equals(DataRow x, DataRow y) 
    { 
     var cols = x.Table.Columns.Count; 
     for (var i = 0; i < cols; i++) 
     { 
      if (Convert.ToString(x[i]) != Convert.ToString(y[i])) 
      { 
       return false; 
      } 
     } 
     return true; 
    } 

    public int GetHashCode(DataRow obj) 
    { 
     var hashCode = -1; 

     var cols = obj.Table.Columns.Count; 
     for (var i = 0; i < cols; i++) 
     { 
      if (hashCode == -1) 
      { 
       hashCode = obj[i].GetHashCode(); 
      } 
      else 
      { 
       hashCode = hashCode^obj[i].GetHashCode(); 
      } 
     } 

     return hashCode; 
    } 
} 
+0

这个答案不是我的解决方案.......我不能使用数据行,我需要读取和比较同一时间,第一行与同一列的第二行,在这种情况下,列数为4我正在使用excel。 – YotiS