我正在审查我正在工作的一个应用程序中连接到数据库的代码,并看到了这个:
if (_dbConnection == null)
_dbConnection = GetConnection();
while (_dbConnection.State == ConnectionState.Connecting)
{
//Do Nothing until things are connected.
}
if (_dbConnection.State != ConnectionState.Open)
_dbConnection.Open();
var command = GetCommand(commandType);
command.Connection = _dbConnection;
return command;
这个while循环让我有点担心。有没有更好的方法来等待连接成功之前什么都不做?
编辑:
连接是通过以下方式获取的
private static IDbConnection GetConnection()
{
return new SqlConnection(ConfigurationManager.ConnectionStrings["CoonectionStringName"].ConnectionString);
}