2015-10-16 189 views
0

我正在通过使用OleDbConnection()读取Excel值并将值保存在mysql中。如何在DataRow中检查行值是否为空c#asp.net

在Excel文件中,如果某个单元格值为空,则会在添加到数据库时显示错误。

所以我想在分配之前验证每个值。我怎样才能做到这一点?

例如,对于该行:

colBasic = row[10].ToString(); 

如果此row[10]为空,则colBasic应为零。

protected void Button1_Click(object sender, EventArgs e) 
{    
      string path = "C:\\Pay.xls"; 
      string query = "SELECT * FROM [Sheet1$]"; 
      OleDbConnection conn = new OleDbConnection(); 

      if (File.Exists(path) == true) 
      { 
       conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = '" + path + "'" + @";Extended Properties=""Excel 8.0;HDR=NO;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0"""; 
       conn.Open(); 

       string ex_id = ""; 
       string colName = ""; 
       string colEmpID = ""; 
       string colBasic = "0"; 
       string colHRA = "0"; 
       string colConveyance = "0"; 

       try 
       { 
        OleDbCommand ocmd = new OleDbCommand(query, conn); 
        OleDbDataReader odr = ocmd.ExecuteReader(); 
        int nFields = odr.FieldCount; 

        DataTable dtable = new DataTable(); 
        dtable.Load(odr); 
        if (dtable.Rows.Count > 0) 
        { 
         DataRow row = dtable.Rows[0]; 

         if (GlobalCS.OpenConnection() == true) 
         {        
           for (int i = 5; i < dtable.Rows.Count; i++) 
           { 
            row = dtable.Rows[i]; 
            ex_id = row[0].ToString(); 
            colEmpID = row[2].ToString(); 
            colName = row[1].ToString(); 
            colBasic = row[10].ToString(); 
            colHRA = row[11].ToString(); 
            colConveyance = row[12].ToString();          

            if (colName != "") 
            { 


              sQuery = "insert into salary (EmployeeID,EmployeeName,Basics,DA,HRA,Conveyance,) values(@a,@b,@c,@d,@e,@f)"; 
                         cmd = new MySqlCommand(sQuery, GlobalCS.objMyCon); 
              cmd.Parameters.AddWithValue("a", colEmpID); 
              cmd.Parameters.AddWithValue("b", colName); 
              cmd.Parameters.AddWithValue("c", colBasic); 
              cmd.Parameters.AddWithValue("d", '0'); 
              cmd.Parameters.AddWithValue("e", colHRA); 
              cmd.Parameters.AddWithValue("f", colConveyance); 
rowsaffected = cmd.ExecuteNonQuery(); 
} //close of if(colName!=0) 
} //close of for loop 

         } // close of if(GlobalCS.Openconnection()) 
        } // close of if(dtable.Rows.Count > 0) 
        GlobalCS.CloseConnection(); 
        conn.Close(); 
       } 
       catch (Exception ex) 
       { 
        string display = ex.Message; 
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true); 

        GlobalCS.CloseConnection();      
        conn.Close(); 
       } 
      } 
      else 
      { 
       string display = "Payslip.xls file not found"; 
       ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true); 

       GlobalCS.CloseConnection();    
       conn.Close(); 
      } 
+0

使用[三元操作符(HTTPS更改线路:// MSDN。 microsoft.com/en-us/library/ty67wk28.aspx):'colBasic = row [10]!= null?行[10] .ToString():“0”;' – markpsmith

+0

不......要获取它为空。当我在观察窗口中看到它时,行[10]显示为{}。 – Anu

+2

'colBasic =!string.IsNullOrEmpty(row [10])?行[10] .ToString():“0”;' – markpsmith

回答

0

这应该工作...

$(document).ready(function() { 
    $(document).keydown(function (event) { 
     if (event.ctrlKey == true && (event.which == '107' || event.which == '109' || event.which == '187' || event.which == '189')) 
     { 
      event.preventDefault(); 
     } 
    }); 
    }); 

    protected void Button1_Click(object sender, EventArgs e) 
    {    
     string path = "C:\\Pay.xls"; 
     string query = "SELECT * FROM [Sheet1$]"; 
     OleDbConnection conn = new OleDbConnection(); 

     if (File.Exists(path) == true) 
     { 
      conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = '" + path + "'" + @";Extended Properties=""Excel 8.0;HDR=NO;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0"""; 
      conn.Open(); 

      string ex_id = ""; 
      string colName = ""; 
      string colEmpID = ""; 
      string colBasic = "0"; 
      string colHRA = "0"; 
      string colConveyance = "0"; 

      try 
      { 
       OleDbCommand ocmd = new OleDbCommand(query, conn); 
       OleDbDataReader odr = ocmd.ExecuteReader(); 
       int nFields = odr.FieldCount; 

       DataTable dtable = new DataTable(); 
       dtable.Load(odr); 
       if (dtable.Rows.Count > 0) 
       { 
        DataRow row = dtable.Rows[0]; 

        if (GlobalCS.OpenConnection() == true) 
        {        
          for (int i = 5; i < dtable.Rows.Count; i++) 
          { 
           row = dtable.Rows[i]; 
           ex_id = row[0].ToString(); 
           colEmpID = row[2].ToString(); 
           colName = row[1].ToString(); 
           colBasic = !string.IsNullOrEmpty(row[10].ToString()) ? row[10].ToString() : "0"; 
           colHRA = row[11].ToString(); 
           colConveyance = row[12].ToString();          

           if (colName != "") 
           { 


             sQuery = "insert into salary (EmployeeID,EmployeeName,Basics,DA,HRA,Conveyance,) values(@a,@b,@c,@d,@e,@f)"; 
                        cmd = new MySqlCommand(sQuery, GlobalCS.objMyCon); 
             cmd.Parameters.AddWithValue("a", colEmpID); 
             cmd.Parameters.AddWithValue("b", colName); 
             cmd.Parameters.AddWithValue("c", colBasic); 
             cmd.Parameters.AddWithValue("d", '0'); 
             cmd.Parameters.AddWithValue("e", colHRA); 
             cmd.Parameters.AddWithValue("f", colConveyance); 
             rowsaffected = cmd.ExecuteNonQuery(); 
           } //close of if(colName!=0) 
        } //close of for loop 

        } // close of if(GlobalCS.Openconnection()) 
       } // close of if(dtable.Rows.Count > 0) 
       GlobalCS.CloseConnection(); 
       conn.Close(); 
      } 
      catch (Exception ex) 
      { 
       string display = ex.Message; 
       ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true); 

       GlobalCS.CloseConnection();      
       conn.Close(); 
      } 
     } 
     else 
     { 
      string display = "Payslip.xls file not found"; 
      ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true); 

      GlobalCS.CloseConnection();    
      conn.Close(); 
     } 

这是你应按照乌拉圭回合例如

colBasic = !string.IsNullOrEmpty(row[10].ToString()) ? row[10].ToString() : "0"; 
+0

!string.IsNullOrEmpty((row [10] .ToString())会在row [10]为null时抛出null引用异常(即使不可能,它看起来不太好,尤其是考虑到它没有必要) ;) – mikus