簡體   English   中英

從excel到sql tabel的數據正在以NULL值導入...為什么?

[英]Data from excel to sql tabel is importing in NULL values…why??

我正在將數據從Excel導入數據庫。 以下是excel列的架構:以下是db .....的架構,並與excel工作表數據完全匹配....................

ID  integer                 
organizationId  integer             
categoryId  integer         
attribute   text          
Y1960       integer or null     
Y1961       integer or null      
Y1962       integer or null       
Y1963       integer or null      
Y1964       integer or null     
Y1965       integer or null       
Y1966       integer or null      
Y1967       integer or null       
Y1968       integer or null      
Y1969       integer or null

在數據庫列中也是如此。 我正在使用以下代碼first 10 columns from excel文件中檢索first 10 columns from excel數據。

OleDbConnection excelConnection =
                        new OleDbConnection(excelConnectionString);

        //Create OleDbCommand to fetch data from Excel
        OleDbCommand cmd = new OleDbCommand
        ("Select [ID],[organizationID],[categoryID],[parentID],[granularityLevel],[attribute],[Y1960],[Y1961],[Y1962],[Y1963],[Y1964],[Y1965] from [Details$]", excelConnection);

        excelConnection.Open();
        OleDbDataReader dReader;
        dReader = cmd.ExecuteReader();

        SqlBulkCopy sqlBulk = new SqlBulkCopy(connectionString);
        sqlBulk.DestinationTableName = "Data";
        sqlBulk.WriteToServer(dReader);

問題是Y1960 column is not copied數據Y1960 column is not copied從excel文件Y1960 column is not copied到數據庫表中。 其他列將被復制,但不會復制Y1960。 那里只有空值。

我過去幾次遇到過此問題,我發現的解決方案是使目標數據類型為浮點型。 這里有一個建議,首先要暫存數據,然后進行轉換,這也可能是一個選項。 從Excel導入SQL Server 2008時轉換數據類型時出錯

我希望Y1960是您的Excel工作表的第一行。

當您在那時執行此復制任務時, first excel sheet row take as title of column這就是為什么不應復制該行的原因。 您必須在工作表頂部再加上一行標題,而不是可以嘗試的行。

我有同樣的問題,兩列沒有正確復制,而是插入了空值。 解決我問題的是在擴展屬性中使HDR = NO。 將所有列設為Varchar。 它適合我的情況,因為我可以將所有事情都做為VC在SQL中進行

謝謝,TP

從Excel移到Sql表時,我的混合數據顯示為Null。 解決此問題的方法是在Excel的連接字符串中添加IMEX選項。 它告訴驅動程序始終將“混合”數據列讀取為文本。

Provider = Microsoft.Jet.OLEDB.4.0;數據源= D:\\ MyExcel.xls;擴展屬性=“” Excel 8.0; HDR =是; IMEX = 1“”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM