C# 引用dll时System.TypeInitializationException异常
我正在尝试以.dll的形式创建一个数据框架,以便在创建新项目时可以引用它,而不是在我创建的每个项目中重新设计轮子 我有一个app.config,其中存储我的SQL连接,一个类使用app.config构建我的SQL ConnectionString(ConnectionString.cs)和一个逻辑类(Logic.cs),该类将构建我从SQL Server需要的任何对象 以下是.dll中的类: ConnectionString.cs:C# 引用dll时System.TypeInitializationException异常,c#,C#,我正在尝试以.dll的形式创建一个数据框架,以便在创建新项目时可以引用它,而不是在我创建的每个项目中重新设计轮子 我有一个app.config,其中存储我的SQL连接,一个类使用app.config构建我的SQL ConnectionString(ConnectionString.cs)和一个逻辑类(Logic.cs),该类将构建我从SQL Server需要的任何对象 以下是.dll中的类: ConnectionString.cs: using System.Configuration; usi
using System.Configuration;
using System.Data.SqlClient;
namespace DataFramework
{
public static class ConnectionStrings
{
static string _liveConnectionString = ConfigurationManager.ConnectionStrings["LiveDataSource"].ConnectionString;
static string _liveMISConnectionString = ConfigurationManager.ConnectionStrings["LiveDataSource_MIS"].ConnectionString;
static string _devConnectionString = ConfigurationManager.ConnectionStrings["DevDataSource"].ConnectionString;
static string _devMISConnectionString = ConfigurationManager.ConnectionStrings["DevDataSource_MIS"].ConnectionString;
public static SqlConnection CreateLiveConnection
{
get { return new SqlConnection(_liveConnectionString); }
}
public static SqlConnection CreateLiveMISConnection
{
get { return new SqlConnection(_liveMISConnectionString); }
}
public static SqlConnection CreateDevConnection
{
get { return new SqlConnection(_devConnectionString); }
}
public static SqlConnection CreateDevMISConnection
{
get { return new SqlConnection(_devMISConnectionString); }
}
}
}
Logic.cs:
using System;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace DataFramework
{
public class Logic
{
SqlConnection liveConnection = ConnectionStrings.CreateLiveMISConnection;
SqlConnection devMISConnection = ConnectionStrings.CreateDevMISConnection;
public bool IsConnecting { get; set; }
public string ConnectionMessage { get; set; }
public async Task<DataTable> ResultDataTable(bool connectToLive, string commandText, CommandType commandType)
{
DataTable dt = new DataTable();
using (SqlCommand command = new SqlCommand())
{
try
{
command.CommandType = commandType;
command.CommandTimeout = 360000000;
switch (connectToLive)
{
case true:
command.CommandText = commandText;
command.Connection = liveConnection;
if (liveConnection.State == ConnectionState.Connecting)
{
IsConnecting = true;
ConnectionMessage = "Connecting to Data Source...";
}
if (liveConnection.State != ConnectionState.Closed)
liveConnection.Close();
if (liveConnection.State != ConnectionState.Open)
{
liveConnection.Open();
IsConnecting = false;
ConnectionMessage = "";
}
break;
case false:
command.CommandType = commandType;
command.CommandText = "";
command.Connection = devMISConnection;
if (devMISConnection.State == ConnectionState.Connecting)
{
IsConnecting = true;
ConnectionMessage = commandText;
}
if (devMISConnection.State != ConnectionState.Closed)
devMISConnection.Close();
if (devMISConnection.State != ConnectionState.Open)
{
devMISConnection.Open();
IsConnecting = false;
ConnectionMessage = "";
}
break;
}
using (SqlDataReader reader = await command.ExecuteReaderAsync())
{
dt.Load(reader);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, "An Error Has Occured", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
finally
{
if (devMISConnection.State != ConnectionState.Closed)
devMISConnection.Close();
if (liveConnection.State != ConnectionState.Closed)
liveConnection.Close();
}
}
return dt;
}
}
}
代码在此处引发异常:
SqlConnection liveConnection = ConnectionStrings.CreateLiveMISConnection;
那么,为什么在初始化类时会出现此问题呢?当您引用另一个项目中的DLL(或项目)时,会使用最顶层项目中的app.config。因此,如果从WinformsApp调用DataFramework,则WinformsApp中需要有正确的配置设置。默认情况下,它将忽略DataFramework中的任何app.config。有时有点令人沮丧!将您的设置从DataFramework app.config复制到WinformsApp.config中,即可正常工作
另一个不相关的观察结果是,您有以下几点:
"SELECT * FROM MIS.dbo.ETL_Table", CommandType.StoredProcedure
命令类型应为文本,而不是存储过程。从另一个项目引用DLL(或项目)时,将使用最顶端项目的app.config。因此,如果从WinformsApp调用DataFramework,则WinformsApp中需要有正确的配置设置。默认情况下,它将忽略DataFramework中的任何app.config。有时有点令人沮丧!将您的设置从DataFramework app.config复制到WinformsApp.config中,即可正常工作
另一个不相关的观察结果是,您有以下几点:
"SELECT * FROM MIS.dbo.ETL_Table", CommandType.StoredProcedure
命令类型应该是文本,而不是存储过程。什么是内部异常?不再需要提供内部异常,@MurrayFoxcroft的解决方案对我有效什么是内部异常?不再需要提供内部异常,@MurrayFoxcroft的解决方案对meSorry有效,我的坏。。。我通常调用存储过程,因为我不喜欢将SQL代码嵌入到我的应用程序中,而且我忘了在这个实例中更改类型。如果你说的是真的,那么我不妨在我开发的每个应用程序中重新设计轮子:(我尝试了你的建议,效果很好。谢谢你的帮助:)对不起,我的错。。。我通常调用存储过程,因为我不喜欢将SQL代码嵌入到我的应用程序中,而且我忘了在这个实例中更改类型。如果你说的是真的,那么我不妨在我开发的每个应用程序中重新设计轮子:(我尝试了你的建议,效果很好。谢谢你的帮助:)