3

http://filehelpers.sourceforge.net/example_sqlstorage_extract.html

上面的示例连接到 SQL Server。

我的要求是连接到 Oracle DB。我在 filehelpers 中找不到可以用来连接到 oracle DB 的类。如果您有任何想法,请提供帮助。非常感谢。

4

1 回答 1

1

您可以使用GenericDatabaseStorage<T>采用任何 ADO.NETIDBConnection类型的类。以下是来自FileHelpers 源的示例。

[TestClass]
public class GenericDatabaseStorageTests
{
    [TestMethod]
    public void CurrencyOracleExtractToFile( )
    {
        GenericDatabaseStorage<OracleConnection, OracleCommand> storage =
            new GenericDatabaseStorage<OracleConnection, OracleCommand>( 
                typeof(TestRecord), 
                "User Id=SHELL;Password=shell;Data Source=ora9dev"
            );

        storage.SelectSql = "SELECT * FROM CURRENCY";
        storage.FillRecordCallback = new FillRecordHandler( FillRecordOrder );

        FileDataLink.EasyExtractToFile( storage, "tempord.txt" );

        FileDataLink link = new FileDataLink( storage );
        link.ExtractToFile( "tempord.txt" );

        TestRecord[] res = (TestRecord[])CommonEngine.ReadFile(typeof(TestRecord), "tempord.txt");

        if ( File.Exists( "tempord.txt" ) )
            File.Delete( "tempord.txt" );

        Assert.AreEqual( 3, res.Length );

        Assert.AreEqual( "AED", res[ 0 ].CurrencyCode );
        Assert.AreEqual( "AFA", res[ 1 ].CurrencyCode );
        Assert.AreEqual( "ALL", res[ 2 ].CurrencyCode );
    }

    public void FillRecordOrder( object rec, object[ ] fields )
    {
        TestRecord record = ( TestRecord )rec;

        record.CurrencyCode = ( string )fields[ 0 ];
        record.Name = ( string )fields[ 1 ];
    }
}
于 2012-07-31T09:08:46.423 回答