I'm working on a database application in Webmatrix. I have a cshtml file which is supposed to update a record in the database. Everything works correctly if I use string concatenation for the SQL command, but I know that's not safe. So, I'm attempting to use parameters instead. But I get an SQL parsing error when I do.
Works:
@{
string dbName = Request["db"];
string tble = Request["t"];
string idName = Request["idn"];
string id = Request["id"];
string field = Request["f"];
string value = Request["v"];
var db = Database.Open(dbName);
var result = db.Execute("UPDATE ["+tble+"] SET ["+field+"]='"+value+"' WHERE ["+idName+"]='"+id+"'");
<text>Result: @result</text>
}
Causes Error:
@{
string dbName = Request["db"];
string tble = Request["t"];
string idName = Request["idn"];
string id = Request["id"];
string field = Request["f"];
string value = Request["v"];
var db = Database.Open(dbName);
var result = db.Execute("UPDATE @0 SET @1=@2 WHERE @3=@4",tble,field,value,idName,id);
@*var result = db.Execute("UPDATE ["+tble+"] SET ["+field+"]='"+value+"' WHERE ["+idName+"]='"+id+"'");*@
<text>Result: @result</text>
}