Skip to content

Commit dc37715

Browse files
Fix the error when execute statements that contains multi-line comments that start with /* and end with */
1 parent a4ae832 commit dc37715

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

SQL Document Builder/TableBuilderForm.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,24 @@ private SqlEditBox? CurrentEditBox
7474
/// </summary>
7575
private static async Task<string> ExecuteScriptsAsync(DatabaseConnectionItem connection, string script)
7676
{
77-
var sqlStatements = Regex.Split(script, @"\bGO\b", RegexOptions.IgnoreCase | RegexOptions.Multiline);
77+
// First, remove multi-line comments that start with /* and end with */
78+
// The RegexOptions.Singleline flag allows '.' to match newline characters
79+
var scriptWithoutComments = Regex.Replace(script, @"/\*.*?\*/", "", RegexOptions.Singleline);
7880

79-
// execute each statement
81+
// Then, split the script into individual SQL statements using 'GO' as a delimiter
82+
var sqlStatements = Regex.Split(scriptWithoutComments, @"\bGO\b", RegexOptions.IgnoreCase | RegexOptions.Multiline);
83+
84+
// Execute each statement
8085
foreach (var sql in sqlStatements)
8186
{
82-
var query = sql.Trim(' ', '\t', '\r', '\n'); // Trim whitespace from the start and end of the SQL statement
87+
var query = sql.Trim(' ', '\t', '\r', '\n'); // Trim whitespace
8388

8489
if (!string.IsNullOrEmpty(query))
8590
{
8691
var result = await SQLDatabaseHelper.ExecuteSQLAsync(query, connection.ConnectionString);
8792
if (result != string.Empty)
8893
{
94+
// Return immediately if an error occurs
8995
return result;
9096
}
9197
}

0 commit comments

Comments
 (0)