2013-10-15 76 views
0

執行SqlBulkCopy將200k +記錄從Oracle移動到SQL。執行WriteToServer時遇到錯誤轉換字符串guid。在Oracle中,該表將guid保存爲nvarchar(50); SQL是一個唯一的標識符。 sql語句沒有什麼特別之處 - 只需從Oracle源表中選擇所有數據即可。SQLBulkCopy Oracle到SQL Guid錯誤

public void BulkCopyFromOrl(OracleDataReader rdr, string targetTableName) 
{ 
    // open Sql connection 
    var _con = GetConnection(); 

    // Sql Bulk Copy instance 
    var _bc = new SqlBulkCopy(_con); 

    // set destination table 
    _bc.DestinationTableName = targetTableName; 

    // set batch size (optional) 
    _bc.BatchSize = 10000; 

    // set time out (optional) 
    _bc.BulkCopyTimeout = 600; 

    // write to server 
    _bc.WriteToServer(rdr); 

    // close and dispose 
    _bc.Close(); 

    // close and dispose 
    Terminate(null, _con); 
} 

public SqlDataReader GetSourceData(string tableName) 
{ 
    // todo: should be parameterized but for demonstration in the console app, 
    // just add columns and table name 
    var sql = string.Format("SELECT {0} FORM {1}", MappedColumns(), tableName); 
    return ExecuteReader(sql); 
} 

回答

0

它是從,不能形成,我認爲:

public SqlDataReader GetSourceData(string tableName) 
    { 
     // todo: should be parameterized but for demonstration in the console app, 
     // just add columns and table name 
     var sql = string.Format("SELECT {0} FROM {1}", MappedColumns(), tableName); 
     return ExecuteReader(sql); 
    } 
+0

你是正確的 - 但是這不是問題;在輸入問題時出現錯字 – gnome

+0

您必須爲您嘗試插入的每一行生成一個新的guid。 – Jonysuise

+0

http://stackoverflow.com/questions/138302/how-can-use-sqlbulkcopy-on-a-table-with-a-guid-primary-key-and-default-newsequen – Jonysuise