linq to sql初步

1在数据库中设计一个表

2设计一个相关的实体类

注意类名和字段的attribute

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Text;
using Life365.Common;

namespace Life365.Entity
{
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
/// 系统日志实体类
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
[Table(Name="dev.T_SYS_LOGGER")]
publicclass LoggerEntity
{
public LoggerEntity()
{ }

privatestring _sl_logid;
private DateTime _sl_createtime = DateTime.Now;
privatestring _sl_userid;
privatestring _sl_username;
private SysEnum.LoggerType _sl_logtype;
//private int _sl_logtype;
privatestring _sl_logurl;
privatestring _sl_modulename;
privatestring _sl_logdetail;
privatestring _sl_ip;
privatestring _sl_appid;

///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
/// 日志编号
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
[Column]
publicstring SL_LogID
{
set { _sl_logid = value; }
get { return _sl_logid; }
}
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
/// 创建时间
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
[Column]
public DateTime SL_CreateTime
{
set { _sl_createtime = value; }
get { return _sl_createtime; }
}
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
/// 操作员编号
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
[Column]
publicstring SL_UserID
{
set { _sl_userid = value; }
get { return _sl_userid; }
}
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
/// 操作人用户名
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
[Column]
publicstring SL_UserName
{
set { _sl_username = value; }
get { return _sl_username; }
}
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
/// 日志类型
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
[Column]
public SysEnum.LoggerType SL_LogType
{
set { _sl_logtype = value; }
get { return _sl_logtype; }
}
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
/// 记录页面
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
[Column]
publicstring SL_LogUrl
{
set { _sl_logurl = value; }
get { return _sl_logurl; }
}
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
///模块名称
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
[Column]
publicstring SL_ModuleName
{
set { _sl_modulename = value; }
get { return _sl_modulename; }
}
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
/// 详细信息
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
[Column]
publicstring SL_LogDetail
{
set { _sl_logdetail = value; }
get { return _sl_logdetail; }
}
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
/// 日志操作人IP
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
[Column]
publicstring SL_IP
{
set { _sl_ip = value; }
get { return _sl_ip; }
}
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
/// 系统编号
///<SUMMARY></SUMMARY><SUMMARY></SUMMARY>
[Column]
publicstring SL_AppID
{
get { return _sl_appid; }
set { _sl_appid = value; }
}
}
}

 

 

3访问数据库的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;
using Life365.Entity;

namespace testweb
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataContext context = new DataContext("Data Source=192.168.3.18;Initial Catalog=dbserver;User ID=sa;Password=allen");
            context.Log = Console.Out;
            var b = from v in context.GetTable().Skip(18).Take(18) select new { v.SL_LogDetail };     
            foreach (var a in b)
            {
                Response.Write(a.SL_LogDetail + "
"); } } } }

这里假设一页18条数据,访问第二页

看来分页的存储过程已经不需要了

另:
此访问用到了数据的延迟加载

并非把所有的字段全部读出来,而是只读了一个字段 SL_LogDetail

posted @ 2009-12-15 08:40  liulun  阅读(617)  评论(1编辑  收藏  举报