c# - Dynamic Sql statement change by variable in method Up -
i insert values postgresql table using code first migration. code far:
public override void up() { var test = "testvalue"; sql(@"insert dbo.""company"" (""id"", ""name"") values ('2', test)"); } this sample code. trying achieve insert precomputed value name column. above code fail message there no column named test intention substitute value of test. please me proper syntax.
you have 2 possibilities either using string concatenation or string.format. if need formatted output e.g. number of decimals in number etc. should use string.format.
var test = "testvalue"; string sqlstring = @"insert dbo.""company"" (""id"", ""name"") values ('2', '" + test + @"')"; sql(sqlstring); var test = "testvalue"; string sqlstring = string.format(@"insert dbo.""company"" (""id"", ""name"") values ('2', '{0}')", test); sql(sqlstring); your sqlstring contain:
insert dbo."company" ("id", "name") values ('2', 'testvalue') after it.
Comments
Post a Comment