所请求的数据库未在配置中定义
本文关键字:配置 定义 请求 数据库 | 更新日期: 2023-09-27 18:02:37
我正在开发的一个应用程序使用Microsoft Practices Enterprise来处理数据库交互。
这一切都很好,但在某些时候它只是停止工作。我能想到的唯一一件事是,我从部署在不同机器上的另一个数据库恢复了我正在使用的数据库(由具有不同帐户的用户访问,而不是我现在使用的帐户)。
我创建了2个小测试(1个解决方案,1个项目有1个类,Program.cs
),第一个作品:
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 | Value1 | Value2 | 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>
我认为如果有任何连接/配置问题,第一个也会失败。我也试过使用空构造函数,但那只是抛出了另一个异常,说String
参数不能是null
或空。
原来微软产品之间存在冲突。在我的例子中,它是ESB Toolkit安装。更多信息请点击此处。
卸载ESB Toolkit对我来说是一种选择。
如果你使用
Database sda = DatabaseFactory.CreateDatabase("DBConnectionString");
确保在App.config
"connectionStrings
add name="DBConnectionString" connectionString="server=DBServerNameHere;database=DatabaseNameHere;Integrated Security=true;" providerName="System.Data.SqlClient"
connectionStrings"