2015-04-14 62 views
0

下面是我的代碼,這裏一切都很好,但創建問題的一件事是,我從Excel表插入數據庫中的屬性沒有得到確切的值。如果它們包含相同的值,循環也從其他表中獲取值屬性名稱。如何通過工作表從Excel中獲取價值?

這裏是我的代碼

string sqlConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + outputFile + ";Extended Properties=Excel 12.0;Persist Security Info=False"; 
        //Create Connection to Excel work book and add oledb namespace 
        OleDbConnection excelConnection = new OleDbConnection(sqlConnectionString); 
        excelConnection.Open(); 
        DataTable dt = new DataTable(); 

        dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
        if (dt == null) 
        { 
         return null; 
        } 

        String[] excelSheets = new String[dt.Rows.Count]; 
        int t = 0; 
        //excel data saves in temp file here. 
        foreach (DataRow row in dt.Rows) 
        { 
         excelSheets[t] = row["TABLE_NAME"].ToString(); 
         t++; 
        } 
        OleDbConnection excelConnection1 = new OleDbConnection(sqlConnectionString); 
        DataSet ds = new DataSet(); 
        for (int i = 0; i <= excelSheets.Length - 1; i++) 
        { 
         string query = string.Format("Select * from [{0}]", excelSheets[i]); 
         using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1)) 
         { 
          dataAdapter.Fill(ds); 
         } 

         //Debug.Write("UserinfoId==" + ds.Tables[0].Rows[0]["UserInfoID"].ToString()); 
         for (int j = 0; j < ds.Tables[0].Rows.Count; j++) 
         { 
          SqlConnection sqlc = new SqlConnection(); 
          sqlc.ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; 
          if (excelSheets[i] == "IdentityUser$") 
          { 
           //Debug.Write("Id==" + ds.Tables[0].Rows[j]["Id"].ToString() + "\n"); 

          } 
          else if (excelSheets[i] == "USERINFO$") 
          {        

           //Debug.Write("UserinfoId==" + ds.Tables[0].Rows[j]["UserInfoID"].ToString()+"\n"); 
          } 

現在,當我從Excel工作表USERINFO $它是從IdentityUser $表中也因爲我是從該表得到UserinfoId太

+0

嘗試將循環內的DataSet ds = new DataSet()移到表單上。我認爲,如果您在數據表中調用2次填充,則會合並這些值,因此您將從表格1中獲取值,並從表格2中獲取值。這就是爲什麼在表格2中沒有值的情況下,您會得到值表1 –

+0

@AndreiNeagu非常感謝它解決了我的問題 –

回答

0

搜索插入userinfoId這解決了我的問題。我已經把DataSet ds = new DataSet();放在for循環裏面了,它可以工作。

string sqlConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + outputFile + ";Extended Properties=Excel 12.0;Persist Security Info=False"; 
         //Create Connection to Excel work book and add oledb namespace 
         OleDbConnection excelConnection = new OleDbConnection(sqlConnectionString); 
         excelConnection.Open(); 
         DataTable dt = new DataTable(); 

         dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
         if (dt == null) 
         { 
          return null; 
         } 

         String[] excelSheets = new String[dt.Rows.Count]; 
         int t = 0; 
         //excel data saves in temp file here. 
         foreach (DataRow row in dt.Rows) 
         { 
          excelSheets[t] = row["TABLE_NAME"].ToString(); 
          t++; 
         } 
         OleDbConnection excelConnection1 = new OleDbConnection(sqlConnectionString);     
         for (int i = 0; i <= excelSheets.Length - 1; i++) 
         { 
          DataSet ds = new DataSet(); 
          string query = string.Format("Select * from [{0}]", excelSheets[i]); 
          using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1)) 
          { 
           dataAdapter.Fill(ds); 
          } 


            for (int j = 0; j < ds.Tables[0].Rows.Count; j++) 
            { 
             SqlConnection sqlc = new SqlConnection(); 
             sqlc.ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; 
             if (excelSheets[i] == "IdentityUser$") 
             {         

             } 
             else if (excelSheets[i] == "USERINFO$") 
             { 
             } 
             } 
            }