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.

string concatenation:

var test = "testvalue"; string sqlstring = @"insert dbo.""company"" (""id"", ""name"") values ('2', '" + test + @"')"; sql(sqlstring); 

string.format:

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

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -