中文
    razor与slq学习笔记
    达人设计 LV.1 收藏 举报
    内容简介:《razor与slq学习笔记》由达人设计整理,简单易懂,适合入门学习。
    2017-06-27 11:51 作者:达人设计 浏览(49828) 评论(0) 喜欢(0)

    1.用 WebGrid 显示数据

    @{

      var db = database.open("database");

      var sql = db.query("select * from table");

      var grid = new webgrid(source: sql);

    }

    @grid.GetHtml(

      tableStyle: "grid",

      headerStyle: "head",

      footerStyle: "footer",

      alternatingRowStyle: "alt",

      columns: grid.Columns(

      grid.Column("Column1","字段1"),

      grid.Column("Column2","字段2"),

      grid.Column("Column3","字段3")

      )

    )

    2.用foreach显示数据

    @{

      var db = Database.Open("database");

      var sql = db.query("select * from table");

      var data = db.Query(sql);  

    }

    @foreach(var item in data)

    {

      <li><a href="#">@item. Column1</a></li>

    }

    3..添加数据

    @{

      var column1 = "";

      var column2 = "";

      var column3 = "";

      if(IsPost){

        column1 = Request.Form["column1"];

        column2 = Request.Form["column2"];

        column3 = Request.Form["column3"];

        var db = Database.Open("database");

        var insertCommand = "INSERT INTO table (Column1, Column2, Column3) VALUES(@0, @1, @2)";

        db.Execute(insertCommand, column1, column2, column3);

        Response.Redirect("~/admin/index");

      }

    }

    4.删除数据

    @{

      var ContentId = "";

      var column2 = "";

      if(!IsPost){

        if(!Request.QueryString["Id"].IsEmpty() && Request.QueryString["Id"].IsInt()){

          ContentId = Request.QueryString["Id"];

          var db = Database.Open("database ");

          var sql = "SELECT * FROM table WHERE Id = @0";

          var data = db.QuerySingle(sql, ContentId);

          if (data != null)

          {

            column2 = data. Column2;

          }

        }

      }

      if(IsPost && !Request["buttonDelete"].IsEmpty()){

        ContentId = Request.Form["ContentId"];

        var db = Database.Open("database ");

        var deleteCommand = "DELETE FROM table WHERE Id = @0";

        db.Execute(deleteCommand, ContentId);

        Response.Redirect("~/admin/index");

      }

    }

    5.编辑数据

    @{

    var ContentId = "";

    var column2 = "";

      if(!IsPost){

        if(!Request.QueryString["Id"].IsEmpty() && Request.QueryString["Id"].IsInt()){

          ContentId = Request.QueryString["Id"];

          var db = Database.Open("database ");

          var sql = "SELECT * FROM table WHERE Id = @0";

          var data = db.QuerySingle(sql, ContentId);

          if (data != null)

          {

            column2 = data. Column2;

          }

        }

      }

      if(IsPost){

        column2 = Request.Form["column2"];

        ContentId = Request.Form["ContentId"];

        var db = Database.Open("database ");

        var updateCommand = "UPDATE table SET>

        db.Execute(updateCommand, column2, ContentId);

        Response.Redirect("~/admin/index");

      }

    }

    6.查询

    @{

      var db = Database.Open("database") ;

      var sql = "SELECT * FROM table";

      var searchTerm = "";

      if(!Request.QueryString["searchTitle"].IsEmpty() ) {

        selectCommand = "SELECT  * FROM table WHERE Title LIKE @0";

        searchTerm = "%" + Request["searchTitle"] + "%";

      }

      var selectedData = db.Query(selectCommand, searchTerm);

      var grid = new WebGrid(source: selectedData,  rowsPerPage: 20);

    }

    <form method="get">

      <input type="text" name="searchTitle" value="@Request.QueryString["searchTitle"]" title="输入标题关键字"/>

      <input type="Submit" name="button" value="搜索"/>

    </form>

    7.sql常用查询语句

    select id,title from table /*查询表中id和tile字段的所有记录*/

    select * from table where /*查询表中字段year值为2008的所有记录*/

    select * from table order by year /*以year字段升序排列 */

    select * from table order by year desc /*以year字段降序排列 */

    select * from table order by date desc ,id asc /*以日期降序,id升序排列 */

    SELECT TOP 10 * FROM table ORDER BY NEWID() /*从Orders表中随机取出10条记录*/

    8.常用技巧

    (1)获取有html或特殊字符:content = Request.Unvalidated.Form["content"];

    (2)时间格式转换:@DateTime.Now.ToString(""yyyy-MM-dd HH:mm:ss")

    (3)带用html标签的字符串输出:@Html.Raw(item.Content)

    (4)截取字符串:@Html.Raw(item.Content.Substring(0,10))

    9. Session

    存值:Session["loginUserId"] = data.Id;

    取值:loginUserId = Session["loginUserId"]

    10.helper 助手(一般放在App_Code文件夹下面多处调用)

    @helper Menu()

    {

      var db = Database.Open("rcdb");

      var sql = "SELECT top 10 * FROM DesignWhere where ";

      var data = db.Query(sql);

      foreach (var item in data)

      {

        <li><a href = "~/@item.Link" rel="@item.Link">@item.Title</a></li>

      }

    }

    输出:@helpers.Menu()

    11.函数(一般放在App_Code文件夹下面多处调用)

    //伪静态--注释:接受页面用 UrlData[0] 代替 Request.QueryString["id"]

    @functions{

      public static string seo(String Page, int id)

      {

        return "~/" + Page + "/" + id;

      }

    }

    输出:

    @foreach (var item in datalist)

    {

      <li><a href = "@Href(razor.seo("detail",item.Id ))" target="_blank">@item.Title</li>

    }

    •  
     点击图片添加 X
     点击图片添加 X