Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 抛出未找到数据库异常_C#_.net_Exception - Fatal编程技术网

C# 抛出未找到数据库异常

C# 抛出未找到数据库异常,c#,.net,exception,C#,.net,Exception,.NET中是否存在DatabaseNotFound的内置异常,我可以将其用于连接到数据库服务器以执行维护任务的程序 我觉得应该有,但我一直没能找到。在下面的代码中,您可以看到我检查app.config数据库中定义的数据库是否存在 XML文件: C代码: 最接近的匹配是 System.Configuration.ConfigurationErrorsException System.Data.DataException 由于您的情况更可能是配置错误指定的无效DB名称,而不是数据库未成功创建的DB错

.NET中是否存在DatabaseNotFound的内置异常,我可以将其用于连接到数据库服务器以执行维护任务的程序

我觉得应该有,但我一直没能找到。在下面的代码中,您可以看到我检查app.config数据库中定义的数据库是否存在

XML文件:

C代码:


最接近的匹配是

System.Configuration.ConfigurationErrorsException System.Data.DataException 由于您的情况更可能是配置错误指定的无效DB名称,而不是数据库未成功创建的DB错误,因此第一个选项会更好


如果您想要一个特定的异常,您应该从其中一个派生并创建自己的自定义异常类。

最接近的匹配项是

System.Configuration.ConfigurationErrorsException System.Data.DataException 由于您的情况更可能是配置错误指定的无效DB名称,而不是数据库未成功创建的DB错误,因此第一个选项会更好


如果您想要一个特定的异常,您应该从其中一个派生并创建您自己的自定义异常类。

因为您使用的是我将使用的,因为这是将抛出的异常。

因为您使用的是我将使用的,因为这是将抛出的异常。

ConfigurationException似乎已被消除。如果没有其他人有更好的建议,我想我会选择DataException。谢谢是的,ConfigurationErrorsException是MS建议使用的异常。ConfigurationException似乎已被删除。如果没有其他人有更好的建议,我想我会选择DataException。谢谢是的,ConfigurationErrorsException是MS建议使用的异常。SqlException的问题是,拦截它的人希望填充严重性、状态和错误。SqlException的问题是,拦截它的人希望填充严重性、状态和错误。
<DatabaseConnection source="myLaptop" username="user" password="pass" defaultDatabase="DB1">
    <database companyIdentTable="tableA" companyIdentField="fieldA">Database_A</database>
    <database companyIdentTable="tableB" companyIdentField="fieldB">Database_B</database>
</DatabaseConnection>
for (int i = 0; i < appSettings.DatabaseConnection.database.Length; i++)
{
    string database = builder.QuoteIdentifier(appSettings.DatabaseConnection.database[i].Value); //Security measure
    command = new SqlCommand("SELECT COUNT(1) FROM master.dbo.sysdatabases WHERE name='" + database + "'", conn);
    if ((int)command.ExecuteScalar() <= 0)
    {
        Log("Database '" + appSettings.DatabaseConnection.database[i].Value + "' was not found. Service shutting down.", true);
        //TODO - throw a more specific exception!
        throw new Exception("Database '" + appSettings.DatabaseConnection.database[i].Value + "' was not found. Service shutting down.");
    }
}