I’d like to execute a fairly large .sql
script. Specifically, I’d like to ensure these backup scripts are available on the server, and I’d like my MVC app to do it.
As such my MVC server, on startup, will instantiate a DbContext
to the current database server, load the sql
file contents using File.ReadAllText
and then pass that to the DbContext.Database.ExecuteSqlRaw
method.
However I’m getting a strange parsing error:
Input string was not in a correct format. Failure to parse near offset 17055. Expected an ASCII digit.
The reason this is strange is because I can run the very same sql
file from SSMS and there it’ll work without any problems.
As far as I can tell the offset is on line 436 of the backup script:
@DirectoryStructure nvarchar(max) = '{ServerName}${InstanceName}{DirectorySeparator}{DatabaseName}{DirectorySeparator}{BackupType}_{Partial}_{CopyOnly}',
Specifically it points to the letter S
in {ServerName}
.
I’ve tried to load the sql
file specifying the UTF8 encoding explicitly but the end result is the same. I don’t think it’s an encoding issue, otherwise I think I’d see errors in SSMS as well.
Are there limits on running .sql
scripts via DbContext.Database.ExecuteSqlRaw
, for example related to length? Or script elements? Is there a way to make this work via EntityFramework or do I need a more “robust” SQL client class for this purpose?
1