Reputation: 1120
I am using new C# 6.0 features String Interpolation for generating SQL statements.
$@"INSERT INTO [dbo].[TableName]([Column1], [Column2]) Values({item.property1}, {item.property2})";
If properties are null then generated SQL is the following
INSERT INTO [dbo].[TableName]([Column1], [Column2]) Values(,)
And this cause an error. (Incorrect SQL).
I need Null instead of empty spaces. Can i somehow achieve this ?
Upvotes: 1
Views: 1708
Reputation: 171206
{Convert.ToString(item.property1) ?? "NULL"}
should do it. This is still broken, because you need very specific formatting for the SQL to come out right. You can solve the formatting yourself.
The usual SQL injection disclaimers have been given in the comments already. This approach is unsalvagable. You need to throw this away.
Upvotes: 6