2013-04-21 63 views
1

這個問題是關於使用https://github.com/maxtoroq/DbExtensions無法查詢使用的OleDb和DbExtensions

DbConnection connection = Database.CreateConnection("name=myconx"); 
var query = SQL.SELECT("u.username").FROM("Users u").WHERE("Id={0}", 1); 
IEnumerable<Users> products = connection.Map<Users>(query); 

下面的代碼返回null,不知道爲什麼?

getDbProviderFactory(connection); 

和這裏的定義沒有關於下一個代碼目的的想法:

static readonly Func<DbConnection, DbProviderFactory> getDbProviderFactory = 
      (Func<DbConnection, DbProviderFactory>)Delegate.CreateDelegate(typeof(Func<DbConnection, DbProviderFactory>), typeof(DbConnection).GetProperty("DbProviderFactory", BindingFlags.Instance | BindingFlags.NonPublic).GetGetMethod(nonPublic: true)); 

,這裏是我的連接字符串

<add name="myconx" connectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=ES;Initial Catalog=TestDB;Data Source=ITA-PC\SQLR2" providerName="System.Data.OleDb"/> 

回答

1

的問題是,OleDbConnection的不會覆蓋DbProviderFactory財產,這也是OdbcConnection的一個問題。我會將這些報告爲錯誤。

如果你的源代碼,你可以嘗試這樣的事:

public static DbProviderFactory GetProviderFactory(this DbConnection connection) { 

    if (connection is OleDbConnection) 
     return OleDbFactory.Instance; 

    return getDbProviderFactory(connection); 
    } 
+0

非常感謝你 – capadleman 2013-04-21 17:20:08