It seems that at some point something happens to the SMO object where it no longer applies to the specified database, despite the database name being the intended object.
To correct this I created a new database object and applied the scripts from there:
Server server = new Server();
server.ConnectionContext.ConnectionString = connectionString;
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
Database db = server.Databases[builder.InitialCatalog];
Then later instead run
db.ExecuteNonQuery(script);