Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to create a data access layer that works with any data provider.

I know it's possible to create a DbCommand using the factory method available on the connection.

objDbCon.CreateCommand();  

However, I could not find anything to create a DbDataAdapter. Is this is a bug in ADO.NET or what?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
381 views
Welcome To Ask or Share your Answers For Others

1 Answer

As of .NET 4.5, when writing provider independent code, you can now use the DbProviderFactories.GetFactory overload that accepts a DbConnection to obtain the correct provider factory from which you can then create a data adapter.

Example:

DbDataAdapter CreateDataAdapter(DbConnection connection)
{
    return DbProviderFactories.GetFactory(connection).CreateDataAdapter();
}

It seems someone on the ADO.NET team read Ian Boyd comment on his answer... :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...