2013-03-07 68 views
0

我正在使用的應用程序使用Microsoft Practices Enterprise來處理數據庫交互。請求的數據庫未在配置中定義

這一切都很好,但在某些時候它只是停止工作。我能想到的唯一的事情是,我從另一個部署在不同計算機上的數據庫(由具有不同於我現在使用的賬戶的用戶訪問)的另一個數據庫恢復了我正在處理的數據庫。

我已經創建了2個個小測試(1個解決方案具有1類,Program.cs 1個項目),首部作品:

 static void test1() 
     { 
      // Create a connection 
      SqlConnection sdwDBConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Test"].ToString()); 

      // Open the connection 
      sdwDBConnection.Open(); 

      // Create a String to hold the query. 
      string query = "SELECT * FROM [TestDB].[dbo].[Test1]"; 

      // Create a SqlCommand object and pass the constructor the connection string and the query string. 
      SqlCommand queryCommand = new SqlCommand(query, sdwDBConnection); 

      // Use the above SqlCommand object to create a SqlDataReader object. 
      SqlDataReader queryCommandReader = queryCommand.ExecuteReader(); 

      // Create a DataTable object to hold all the data returned by the query. 
      DataTable dataTable = new DataTable(); 

      // Use the DataTable.Load(SqlDataReader) function to put the results of the query into a DataTable. 
      dataTable.Load(queryCommandReader); 

      // Example 1 - Print your Column Headers 
      String columns = string.Empty; 
      foreach (DataColumn column in dataTable.Columns) 
      { 
       columns += column.ColumnName + " | "; 
      } 
      Console.WriteLine(columns); 

      // Close the connection 
      sdwDBConnection.Close(); 
      System.Console.Read(); 
     } 

產量:

ID1 |值1 |值2 | Value3 |

但第二個失敗:

 static void test2() 
     { 
      try 
      { 
       Database db = DatabaseFactory.CreateDatabase("Test"); //Line 59 
       using (DbCommand cmd = db.GetSqlStringCommand("SELECT * FROM [TestDB].[dbo].[Test1]")) 
       { 
        using (IDataReader reader = cmd.ExecuteReader()) 
        { 
         while (reader.Read()) 
         { 
          System.Console.WriteLine("{0} {1} {2}", reader.GetInt64(0)  
          , reader.GetString(1) 
          , reader.GetString(2)); 
         } 
        } 
       } 
      } 
      catch (Exception e) 
      { 
       System.Console.WriteLine(e.Message + "\n" + e.StackTrace); 
      } 
      finally 
      { 
       System.Console.Read(); 
      } 
     } 

產量:

The requested database Test is not defined in configuration. 
    at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp[T](IReadWriteLocator locator, ILifetimeContainer 
lifetimeContainer, String id, IConfigurationSource configurationSource) 
    at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp[T](String id, IConfigurationSource configuration 
Source) 
    at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase(String name) 
    at ....Program.test2() in ...\Program.cs:line 59 

這是我的app.config

<?xml version="1.0"?> 
<configuration> 
    <configSections> 
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />  
    </configSections> 
    <dataConfiguration defaultDatabase="Test" /> 
    <connectionStrings>  
    <add name="Test" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=SSPI; Connect Timeout=120" providerName="System.Data.SqlClient" />  
    </connectionStrings> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> 
    </startup> 
    <system.web> 
    <membership defaultProvider="ClientAuthenticationMembershipProvider"> 
     <providers> 
     <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" /> 
     </providers> 
    </membership> 
    <roleManager defaultProvider="ClientRoleProvider" enabled="true"> 
     <providers> 
     <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" /> 
     </providers> 
    </roleManager> 
    </system.web> 
</configuration> 

認爲,如果有w ^這裏任何連接/配置問題的第一個也會失敗。我也嘗試過使用空的構造函數,但這只是引發另一個異常,說明String參數不能爲null或爲空。

任何有關這方面的意見將不勝感激。

+0

此設置從文檔:「DatabaseFactory.CreateDatabase(String)方法用於調用指定的數據庫服務對象讀取服務設置從ConnectionSettings.config文件。「 我從來沒有聽說過ConnectionSettings.config,但您是否嘗試創建ConnectionSettings.config? – 2013-03-07 09:08:19

+0

@JoffreyKern不是。我從來沒有聽說過它,想看看是否有什麼我打破了,而不是試圖添加一些東西來嘗試解決問題。 – npinti 2013-03-07 09:37:19

回答

1

事實證明,微軟產品之間存在衝突。就我而言,這是ESB Toolkit的安裝。更多信息請見here

在我的情況下,卸載ESB工具包就是要走的路。

1

如果您在使用

Database sda = DatabaseFactory.CreateDatabase("DBConnectionString"); 

請確保您有在App.config

"connectionStrings 
    add name="DBConnectionString" connectionString="server=DBServerNameHere;database=DatabaseNameHere;Integrated Security=true;" providerName="System.Data.SqlClient" 
    connectionStrings"