2015-05-19 67 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