我正在处理的应用程序是使用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
或为空。
对此有任何见解将不胜感激。
答案 0 :(得分:1)
事实证明,Microsoft产品之间存在冲突。在我的例子中,它是ESB Toolkit的安装。有关详细信息,请here。
卸载ESB Toolkit就在我的情况下,还有就行了。
答案 1 :(得分: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"