2016-04-15 37 views
1

我在这一行得到一个TypeInitilaizingExceptions。TypeInitializing异常

System.Console.Title = Translator.Translate(Constant.C_PROGRAM_NAME, CurrentLanguage); 

内部异常是

"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)" 

这不会发生的每一次。有时候我会翻译,有时候应用程序会抛出一个错误。

Translator.Translate是做什么的?

public static class Translator 
{ 
    static SQLDAL m_sqlDal = new SQLDAL(); 
    public static string Translate(string translationString, Language language) 
    { 
     translationString = m_sqlDal.GetTranslationFromString(translationString, language); 

     return translationString; 
    } 
} 

它调用方法GetTranslationString。

GetTranslationString在其中有一个try catch块=>异常必须来自我的Translator类。

但这是我的下一个问题:我无法调试这个类。该课程在相同的解决方案中,但在不同的项目中。该DLL是最新的,上一次半小时前重建。

在SQL DAL的构造函数中,我正在搜索一个SQL serer,如果有一台服务器在这台计算机上,它将连接到它,除非没有手动定义其他SQL服务器。我有一个从MS_SQL 2014安装的本地数据库,我的服务器浏览器打开,我有任何访问权限和读/写权限(数据库和服务器的dbowner或sysadmin)。

它从来没有坠毁过。就在今天它开始崩溃,这个类的代码没有改变一段时间...

我的问题: 我该如何解决这个错误,使我的应用程序再次伟大?

+1

您正在错误观察我认为。这不是代码问题。这似乎是连接到您的SQL服务器的问题。确保你可以通过Management Studio或其他东西访问你的SQL服务器。 – Icemanind

+0

我已经完成了这个。我可以写入我的数据库。我在我的SQL管理工作室和我的应用程序中都使用了集成安全性。 – Patrick

回答

0

这是一个TypeInitialization异常。检查正在构建类型时正在构建的SQLDAL类。处理你的例外,继续前进。

或者,您可能需要延迟加载SQLDAL字段,以解决失败并正常处理它们。直线上的静态字段不适合初始化iffy类。