溫馨提示×

在C#中將oledbdatareader轉(zhuǎn)換為dbdatareader

c#
小云
95
2023-09-26 03:02:25
欄目: 編程語言

在C#中將OleDbDataReader轉(zhuǎn)換為DbDataReader,可以使用DbProviderFactory類的CreateCommand和CreateDataAdapter方法來實現(xiàn)。以下是一個示例代碼:

using System.Data.Common;
using System.Data.OleDb;
// 創(chuàng)建OleDbDataReader
OleDbConnection oledbConnection = new OleDbConnection(connectionString);
OleDbCommand oledbCommand = new OleDbCommand(query, oledbConnection);
OleDbDataReader oledbDataReader = oledbCommand.ExecuteReader();
// 轉(zhuǎn)換為DbDataReader
DbProviderFactory factory = DbProviderFactories.GetFactory(oledbConnection);
DbCommand dbCommand = factory.CreateCommand();
dbCommand.Connection = oledbConnection;
dbCommand.CommandText = query;
DbDataReader dbDataReader = dbCommand.ExecuteReader();

在上述代碼中,首先創(chuàng)建了一個OleDbDataReader對象。然后通過DbProviderFactory類的CreateCommand方法創(chuàng)建一個DbCommand對象,并設置其Connection和CommandText屬性。最后通過DbCommand的ExecuteReader方法獲取一個DbDataReader對象,即將OleDbDataReader轉(zhuǎn)換為DbDataReader。

0