我们在 MVC5 项目中使用嵌入式 Firebird V3.0.2
问题:当我们更新软件时,我们想在创建新表之前找出表是否存在。因此,我们只需在所需的表上进行选择,如果出现带有 sql 错误代码的异常-204
,则该表不存在。但是如何解决错误代码-204
?
代码:
var lFbCommand = new FbCommand();
lFbCommand.Connection = "MyConnectionstring";
lFbCommand.Connection.Open();
// check if table exists by an select query
lFbCommand.CommandText = string.Format("SELECT FIRST 1 * FROM {0}", "MyTableName");
try
{
pFbCommand.ExecuteNonQuery();
}
catch (FirebirdSql.Data.FirebirdClient.FbException e)
{
if(e.? == -204) // -204 seem to be the errorcode for "Table does not exists."
{
// table does not exist.
}
else
{
throw;
}
}
注意:我们要防止解析异常中的消息文本。我们想为这个错误获得一个明确的标志(例如,通过任何属性的错误代码)。