开发者

Cannot get the current user ID from an SQL statement using Razor in WebMatrix

开发者 https://www.devze.com 2023-02-11 21:59 出处:网络
I\'m learning razor syntax, using the startersite in WebMatrix and trying to display some data from a table. I want to select only the data from the logged in user, so in effect want to say

I'm learning razor syntax, using the startersite in WebMatrix and trying to display some data from a table. I want to select only the data from the logged in user, so in effect want to say

...WHERE UserId =@WebSecurity.CurrentUserId";

but that doesn't work.

Using the @0 method doesn't work for me either - I get an 'A parameter is missin开发者_开发知识库g. [ Parameter ordinal = 1 ]' error.

Do I have to set a variable then use the @0 / @1 / @whichever way? If so - what's wrong with this code?

@{
//Is the user logged in?
WebSecurity.RequireAuthenticatedUser();

//Get the user's data

var TheUser = WebSecurity.CurrentUserId;

   var db= Database.Open("StarterSite");
   var sqlQ = "SELECT * FROM Days WHERE UserId =@0";
   var data = db.Query(sqlQ);
   }

I'm sure this is very much a beginners issue but I'm just trying to get started. Any help greatly appreciated.


You need to pass the value to the query
documentation

var data = db.Query(sqlQ, TheUser);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号