2016-07-26 32 views
0

我試圖創建一個.dll的形式的數據框架,以便我可以在創建新項目時參考它,而不是重新發明與我創建的每個項目的車輪。System.TypeInitializationException引用DLL時

我有一個app.config用於存儲我的SQL連接,一個使用app.config構建我的SQL ConnectionString(ConnectionStrings.cs)和一個Logic類(Logic.cs)的類,它將構建任何我需要從SQL Server中獲取對象。

下面是.dll文件的類:

ConnectionStrings.cs:

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; 

    } 
} 
} 

包括我這個DLL作爲應用程序的參考,我正在寫作:

using System.Data; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using DataFramework; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
    DataTable dt = new DataTable(); 
    DataFramework.Logic logic = new Logic(); 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private async void Form1_Load(object sender, EventArgs e) 
    { 
     dt = await Task.Run(() => logic.ResultDataTable(true, "SELECT * FROM MIS.dbo.ETL_Table", CommandType.StoredProcedure)); 
    } 
} 
} 

代碼拋出異常的位置:

SqlConnection liveConnection = ConnectionStrings.CreateLiveMISConnection; 

那麼,爲什麼,當我初始化類,我能得到這個問題呢?

+0

什麼是內部異常? – Juan

+0

無需再提供InnerException,@ MurrayFoxcroft的解決方案爲我工作 – GrammatonCleric

回答

2

當您從另一個項目中引用DLL(或項目)時,將使用最頂端項目的app.config。所以,如果你的WinFrameApp被調用了你的DataFramework,那麼你的WinformsApp需要在其中有正確的配置設置。默認情況下,它將忽略DataFramework中的任何app.config。有時有點令人沮喪!將您的設置從您的DataFramework app.config複製到WinformsApp app.config,它將起作用。

另一個無關的觀察是,您具備以下條件:

"SELECT * FROM MIS.dbo.ETL_Table", CommandType.StoredProcedure 

命令類型應該是文本,而不是一個存儲過程。

+0

對不起,我的壞... 我通常會調用存儲過程,因爲我不喜歡將SQL代碼嵌入到我的應用程序中,並且我忘記更改類型這個例子。如果你說的是真的,那麼我不妨重新發明每個應用程序開發車輪:( – GrammatonCleric

+0

我試過你的建議,它工作正常。感謝您的幫助:) – GrammatonCleric