Permalink
Browse files

Use Convert.ToBoolean to parse returned scalar from database when exp…

…ecting boolean value
  • Loading branch information...
1 parent d13bd40 commit da9fc621b4e6f35fb6775f411127d0a1594ce7fa Marko Lahma committed Jan 9, 2012
@@ -2,4 +2,6 @@ USE master
IF NOT EXISTS(SELECT * FROM sys.databases WHERE [name] = '{{DatabaseName}}')
BEGIN
CREATE DATABASE {{DatabaseName}}
+ -- signal success
+ SELECT 1
END
@@ -103,22 +103,19 @@ public virtual bool create_database_if_it_doesnt_exist(string custom_create_data
{
foreach (var sql_statement in StatementSplitter.split_sql_on_regex_and_remove_empty_statements(create_script, sql_statement_separator_regex_pattern))
{
- var return_value = run_sql_scalar(sql_statement, ConnectionType.Admin);
//should only receive a return value once
+ var return_value = run_sql_scalar_boolean(sql_statement, ConnectionType.Admin);
if (return_value != null)
{
- database_was_created = (bool)return_value;
+ database_was_created = return_value.Value;
}
}
}
else
{
- var return_value = run_sql_scalar(create_script, ConnectionType.Admin);
//should only receive a return value once
- if (return_value != null)
- {
- database_was_created = (bool)return_value;
- }
+ var return_value = run_sql_scalar_boolean(create_script, ConnectionType.Admin);
+ database_was_created = return_value.GetValueOrDefault(false);
}
}
catch (Exception ex)
@@ -131,6 +128,16 @@ public virtual bool create_database_if_it_doesnt_exist(string custom_create_data
return database_was_created;
}
+ private bool? run_sql_scalar_boolean(string sql_to_run, ConnectionType connection_type)
+ {
+ var return_value = run_sql_scalar(sql_to_run, connection_type);
+ if (return_value != null && return_value != DBNull.Value)
+ {
+ return Convert.ToBoolean(return_value);
+ }
+ return null;
+ }
+
public void set_recovery_mode(bool simple)
{
try

0 comments on commit da9fc62

Please sign in to comment.