2014-06-16 101 views
0

我正在使用控制檯應用程序腳本將Excel文件導入到Sql Server數據庫中。當我選擇從Excel工作表中只有兩列,LocalSKU庫存量將Excel導入到Sql Server數據庫時將數據類型nvarchar轉換爲數字時出錯

當我選擇幾個專欄中,我得到這個錯誤

private static void ImportToSql(string ssqlconnectionstring, string query) 
    { 
     string query = "MERGE Inventory AS target USING (select LocalSKU,ItemName, QOH,ISNULL(Price,0.00),Discontinued,Integer2,Integer3 from @source) as source ON (source.LocalSKU = target.LocalSKU) WHEN MATCHED THEN UPDATE SET ItemName=source.ItemName,Price=source.Price,Discontinued=source.Discontinued,Integer2=source.Integer2,Integer3=source.QOH;"; 
     string excelfilepath = Environment.CurrentDirectory + @"\Sheet1.csv"; 
     // make sure your sheet name is correct, here sheet name is sheet1, so you can change your sheet name if have different 
     // string myexceldataquery = "select LocalSKU,ItemName,QOH,Price,Discontinued,Barcode,Integer2,Integer3,SalePrice,SaleOn,Price2 from [sheet1$]"; 
     string myexceldataquery = "select LocalSKU,ItemName, QOH,Price,Discontinued,Integer2,Integer3 from [sheet1$]"; 

     try 
     { 

      // string sexcelconnectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelfilepath + ";Extended Properties=Excel 12.0;"; 

      string sexcelconnectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" + excelfilepath + "; Extended Properties=\"Excel 12.0; HDR=Yes; IMEX=2; ImportMixedTypes=Text;\""; 
      //string ssqlconnectionstring = "Data Source=User-PC\\Dell;Trusted_Connection=True;DATABASE=TestStore;CONNECTION RESET=FALSE"; 

      SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring); 

      //series of commands to bulk copy data from the excel file into our sql table 
      OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring); 
      OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn); 

      oledbconn.Open(); 

      OleDbDataReader dr = oledbcmd.ExecuteReader(); 
      SqlCommand sqlcmd = new SqlCommand(@query, sqlconn); 

      SqlParameter param; 
      param = sqlcmd.Parameters.AddWithValue("@source", dr); 
      param.SqlDbType = SqlDbType.Structured; 

      param.TypeName = "dbo.DTTOW"; 
      sqlconn.Open(); 
      sqlcmd.ExecuteNonQuery(); 
      sqlconn.Close(); 

      oledbconn.Close(); 

      Console.WriteLine(".xlsx file imported succssessfully into database. /r /n "); 

     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
    } 

此代碼的工作。 「」將數據類型nvarchar轉換爲數字時出錯。

我使用用戶定義的數據類型:

CREATE TYPE [dbo].[DTTOW] AS TABLE(
    [LocalSKU] [varchar](200) NOT NULL, 
    [ItemName] [varchar](200) NULL, 
    [QOH] [int] NULL, 
    [Price] [decimal](19, 4) NULL, 
    [Discontinued] [bit] NULL, 
    [Integer2] [int] NULL, 
    [Integer3] [int] NULL 

+1

你有什麼,在Excel中價格列非數字值替換非數值?任何與空格,字母,null或空字符串? –

+0

是的..有你的觀點..你能吸引我如何處理這些..? – user3590485

+0

我發佈了一個答案哦,如何處理它們。 –

回答

1

從結構有Excel文件的地方搞砸價格SQL DDL的外觀。你可以運行一個宏或腳本來查看價格欄中的所有單元格是否實際可兌換?你可以ise = ISNUMBER()函數。也容易和骯髒的方式 - 嘗試編輯=價格列底部的SUM()。我想知道是否有一些「聰明的褲子」以' - '或'$'字符嵌入到Price列的單元格中。:)恐怕所有Excel單元格的默認類型都是OLE-DB感知中的nvarachar。

就我個人而言,我會使用Excel inter-op來吸取信息,而不是抵抗像你這樣的隱式轉換錯誤。這是一個慢一點,但沒有你想知道在一個「黑匣子」裏面出錯的是OLE-DB for Excel的問題。

1

您可以在查詢中從excel中選擇一些數值類型。對於價格字段,你可以在查詢更改爲以下,以保證列始終數字,用0

select iif(isnumeric(Price), Price, 0) as Price from [sheet1$] 
相關問題