2013-01-04 23 views
1

我有一個將數據庫調用結果導出到CSV文件的過程。數據類型和文件格式必須匹配特定格式,以便文件可以上傳到外部系統。這個過程在ASP.NET(.NET 2.0)網絡表單上運行了大概7到8年,並且突然(過去6-18個月的某個時間)它不像以前那樣工作。也許在客戶端服務器上安裝了.NET 4.0之後,或者可能在某些其他框架更新(?)或Windows Update(?)或提供程序更新(?)之後。我們的DLL在幾年內沒有改變。我想用最少量的黑客攻擊和削減手段修復這個傳統流程。由ReadXmlSchema定義的DataSet:填充後爲什麼列數據類型會改變?

有三種數據類型被導出:整數,字符串和小數。 問題是現在所有的整數列都被導出爲小數。 CSV導出庫查看列的數據類型以確定正確的輸出格式,因此在填充之前使用XSD文件來定義我的DataSet。下面是XSD文件的簡化示例:

<?xml version="1.0" standalone="yes"?> 
<xs:schema id="FDSR" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> 
    <xs:element name="FDSR" msdata:IsDataSet="true" msdata:Locale="en-CA"> 
    <xs:complexType> 
     <xs:choice maxOccurs="unbounded"> 
     <xs:element name="TBLEXPORT"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="INTCOLUMN1" type="xs:integer" minOccurs="0" /> 
       <xs:element name="STRCOLUMN2" type="xs:string" minOccurs="0" /> 
       <xs:element name="DBLCOLUMN3" type="xs:decimal" minOccurs="0" /> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     </xs:choice> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

爲用於保留的數據被裝載之後的每一列中定義的數據類型,但現在它們是由數據加載復位。例如:

Dim ds as New DataSet 
ds.ReadXmlSchema("MyFile.xsd") 

' Breakpoint here: 
' ds.Tables(0).Columns(0).DataType shows: {Name = "Int64" FullName = "System.Int64"} 

Dim db as New DatabaseCall("my db call...")  
ds = db.ReturnData() 

' Breakpoint here: 
' ds.Tables(0).Columns(0).DataType now shows: {Name = "Decimal" FullName = "System.Decimal"} 

GenerateCSVOutput(ds) 

我怎麼能強迫integer列保持數據庫調用後integer?或者在填充數據集後,如何更改數據類型?

此代碼已被簡化發佈,但基本上db.ReturnData()正在調用Oracle存儲過程來執行一些處理並使用System.Data.OracleClient.OracleDataAdapter.Fill(dataset)來返回數據以填充DataSet。 Oracle中沒有integer列,因此源表的列定義爲NUMBER(1,0)。它肯定會輸出正確的精度,我只是不明白爲什麼DataSet中的列類型在顯式定義爲整數時突然改變。不幸的是,CSV文件需要被上傳到外部的政府系統,該系統將不接受1.01 ...

回答

1

解決方法:Clone the DataSet, change the data type, copy the data

Dim dsExport as New DataSet 
'dsExport.ReadXmlSchema("MyFile.xsd") ' Don't bother, this no longer works 

Dim db as New DatabaseCall("my db call...")  
dsExport = db.ReturnData() 

' Clone the structure of dsExport, while empty change the datatype(s) as required, then copy the data in 
Dim dsClone As DataSet = dsExport.Clone 
dsClone.Tables("tblExport").Columns("INTCOLUMN1").DataType = System.Type.GetType("System.Int32") 
For Each row As DataRow In dsExport.Tables("tblExport").Rows 
    dsClone.Tables("tblExport").ImportRow(row) 
Next 

GenerateCSVOutput(dsClone) 
相關問題