WebMatrix返回WebMatrix.Data.DynamicRecord而不是表中的数据

时间:2016-07-29 10:53:00

标签: razor webmatrix-3

我对WebMatrix比较陌生,希望创建一个显示数据库数据的页面。请参阅下面的代码。它返回WebMatrix返回WebMatrix.Data.DynamicRecord和每个单元格上的列名称

@{
    var db = Database.Open("TimesheetsEdit");
    var selectQueryString = "SELECT * FROM CommsTime ORDER BY [Client name]";
    var result = db.Query(selectQueryString);

}

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Timesheet Edit Form</title>
          <meta name="viewport" content="width=960">
        <link rel="stylesheet" type="text/css" href="scripts/wpstyles.css">
         <style>
         table, th, td {
         border: solid 1px #bbbbbb;
         border-collapse: collapse;
         padding: 2px;
       }
    </style>

    </head>
    <body>
        <h1>Timesheet Edit Form</h1>
   <table style="width: 100%" border:"0" cellspacing:"0" cellpadding:"0">

       <thead>
           <tr>
               <th>Client Name</th>
               <th>Client Short Name</th>
               <th>Project name</th>
               <th>Project number</th>
               <th>Project Type</th>
               <th>Task Billable</th>
               <th>Billing Rate</th>
               <th>CRM Ref</th>
               <th>Travel Site</th>
               <th>Timesheet No</th>
               <th>Commision Hrs</th>
               <th>Commision Rate</th>
           </tr>
       </thead>
       <tbody>
           @foreach(var row in result)

           {
            <tr>
               <td>@row.[Client name]</td>
               <td>@row.[Client short name]</td>
               <td>@row.[Project name]</td>
               <td>@row.[Project number]</td>
               <td>@row.[Project type]</td>
               <td>@row.[Task billable]</td>
               <td>@row.[Billing Rate]</td>
               <td>@row.[CRM Ref:]</td>
               <td>@row.[Travel Site]</td>
               <td>@row.[Timesheet No]</td>
               <td>@row.[Commision Hrs]</td>
               <td>@row.[Commision Rate]</td>


            </tr>
           }
              </tbody>
   </table>        
    </body>
</html>

这是在Webmatrix 3上

Here is the output on the attached image

1 个答案:

答案 0 :(得分:0)

摆脱括号,表中的列名不能有空格。此外,最好在下面进行查询以帮助防止sql注入。

var db = Database.Open("TimesheetsEdit");
var selectQueryString = "SELECT * FROM CommsTime ORDER BY @0";
var clientname = "Clientname";
var result = db.Query(selectQueryString, clientname);

<td>@row.Clientname</td>
           <td>@row.Clientshortname</td>
           <td>@row.Projectname</td>
           <td>@row.Projectnumber</td>
           <td>@row.Projecttype</td>
           <td>@row.Taskbillable</td>
           <td>@row.BillingRate</td>
           <td>@row.CRMRef</td>
           <td>@row.TravelSite</td>
           <td>@row.TimesheetNo</td>
           <td>@row.CommisionHrs</td>
           <td>@row.CommisionRate</td>