ExecuteSqlRawAsync Throws 'Input string was not in a correct format' Exception

Viewed 57

I am writing a script to execute a bunch of SQL statements in the .NET 6 Application.

The script format looks like

SET IDENTITY_INSERT [dbo].[Books] ON 
INSERT [dbo].[Books] ([id], [title], [script]) VALUES (783, 'Harry Potter', N'[{ 
"title": "Harry Potter" }]')
INSERT [dbo].[Books] ([id], [title], [script]) VALUES (784, 'Harry Potter 2', N'[{ 
"title": "Harry Potter 2" }]')
SET IDENTITY_INSERT [dbo].[Books] OFF 

I am using the code to execute the dump script file

 await context.Database.ExecuteSqlRawAsync(script);

When I executed it, it returns

Input string was not in a correct format

If I remove Json from SQL statement

N'[{ "title": "Harry Potter" }]'

Then it's executed fine. Can anyone help how to fix this issue?

1 Answers

The problem is that you could not write a string out of ' , try to write json in query like this:

'N[{ "title": "Harry Potter" }]'

Because the string should be between '' like this 'json string you want'

Related