我需要在 MySQL 远程数据库中注册我的网页 aspx 上的用户访问权限。 但是这个MySQL远程数据库可能不可用。
我已经尝试过这段代码,但是如何执行bool IsServerConnected()方法中的RegisterUSer()方法?
public bool IsServerConnected()
{
using (var l_oConnection =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
try
{
l_oConnection.Open();
return true;
}
catch (OdbcException)
{
return false;
}
}
}
private void RegisterUSer()
{
using (OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
string sql = @String.Format(" INSERT IGNORE INTO tbl_user ");
sql += String.Format(" ... ");
using (OdbcCommand command =
new OdbcCommand(sql, myConnectionString))
{
try
{
command.Connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
command.Connection.Close();
}
}
#编辑01
错误:
The type or namespace name 'resultType' could not be found (are you missing a using directive or an assembly reference?)
最佳答案
您可以只执行一个“包装”方法,该方法首先调用 IsServerConnected() ,然后根据返回的 bool 值调用 RegisterUSer() 或在数据库不可用时抛出错误。
快速而肮脏的伪代码
private resultType ChickenWrapMethod()
{
if (!IsServerConnected())
{
//Throw some error here and exit
}
RegisterUSer()
}
顺便说一句...在我看来,您应该考虑从方法中打开 sql 连接,以便两个操作可以共享它
关于c# - 如何检查远程数据库MYSQL是否可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54627620/