2011-12-29 37 views
0

我有一個Access VBA項目,我指的是使用C#編寫的COM Interop .TLB。此C#代碼只是查詢SQL Server數據庫並通過簡單的LINQ-to-Entity查詢返回值。訪問VBA中的實體框架錯誤 - 「在配置中找不到指定的命名連接...」

我得到這個問題提到了同樣的錯誤:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid

然而,於我而言,這是一個訪問VBA中引用我的.NET 4.0 TLB中的.ADP的應用,而不是另一個.Net項目。

我知道,如果它是另一個.Net項目,我可以在其app.config或web.config中添加EF連接字符串XML。但是,如果我的「調用」應用程序是Access 2003 VBA,那麼該修復程序是什麼?

這裏的VBA代碼調用的.Net代碼

Dim CandidatePassword As String 
Dim abc As New MISHash.Password 

Dim PasswordStatus As Boolean 
CandidatePassword = InputBox("Enter your password") 
PasswordStatus = abc.IsValidPassword("myusername", CandidatePassword) ' FAILS HERE 
If PasswordStatus Then 
    MsgBox "Password valid." 
Else 
    MsgBox "Password failed." 
End If 

請幫助。謝謝。

更新:這裏是我的C#代碼

using System.Linq; 
using System.Runtime.InteropServices; 
namespace MISHash 
{ 

public class Password 
{ 

    public Password() 
    { 

    } 

    [ComVisible(true)] 
    public void HashAndSave(string SomePassword) 
    { 
     string hashed = BCrypt.HashPassword(SomePassword, BCrypt.GenerateSalt(12)); 
     //save the hashed password in the database 
    } 

    [ComVisible(true)] 
    public bool IsValidPassword(string CandidateUserName, string CandidatePassword) 
    { 

     string OriginalHashedPassword; 
     using (MyDBEntities mycontext = new MyDBEntities()) 
     { 
      OriginalHashedPassword = (from usr in mycontext.Users 
             where usr.UserName.Equals(CandidateUserName) 
             select usr.Password).FirstOrDefault(); 
     } 
     bool matches = BCrypt.CheckPassword(CandidatePassword, OriginalHashedPassword); 
     return matches; 
    } 
} 
} 

回答

1

看到這個類似的問題:
Can I use/access the app.config from .net code, when called via COM

這兩個似乎是您最好的選擇:

  1. 手動創建一個次要的AppDomain
  2. 轉換爲VSTO項目

編輯

您也可以嘗試通過硬編碼的連接字符串中的構造:

MyDBEntities mycontext = new MyDBEntities("Server=.\SQLEXPRESS;Database=School;Trusted_Connection=true;Integrated Security=True;MultipleActiveResultSets=True")) 
+0

我的連接字符串看起來是這樣的:元= RES:// * /爲MyModel。 csdl | res://*/MyModel.ssdl | res://*/MyModel.msl; provider = System.Data.SqlClient; provider connection string =「Data Source = DBSERVER; Persist Security Info = True; User ID = dbuser ; Password = dbpwd; MultipleActiveResultSets = True;連接超時= 120;應用程序名稱= EntityFramework「我們如何將這個傳遞給構造函數? – FMFF 2012-01-02 19:55:57

+0

我嘗試了在您的編輯下建議的方法;而不是連接字符串,我建立和使用EntityConnection連接對象建議[這裏](http://msdn.microsoft.com/en-us/library/bb738533.aspx),它的工作。非常感謝你給我指路。 – FMFF 2012-01-02 22:05:17

相關問題