ORM, Linq to sql增删改查

对‘Microsof SQL Server’数据源的‘用于 SQL server 的.NET Framework 数据提供程序’支持。
对‘Microsof SQL Server’数据源的‘用于 OLE DB 的.NET Framework 数据提供程序’不支持。
对‘Microsoft ODBC 数据源‘用于 用于 ODBC 的 .NET Framework 数据提供程序’不支持。
对‘Microsoft Access 数据库文件’数据源的‘用于OLE DB的.NET Framework 数据提供程序’不支持。
即,对SQL Server的专用数据提供程序支持,对OLE DB、ODBC数据提供程序不支持,对Oralce专用属于提供程序未做测试

建议:

因 为Linq to sql 的数据持久方式不能跨数据库平台,因此后台如果是sql server 数据库,可以使用此orm,调用过程最好是‘业务逻辑层’===》‘数据访问层’====》‘此Linq to sql 的orm对象’,这样,如果要跨数据库,则需要重写‘数据访问层’(此数据访问层相当于一个代理),对上面的各层不会产生影响。

2、调用:

    
    protected void Page_Load(object sender, EventArgs e)
    {
        DataPersonDataContext dc = new DataPersonDataContext();

        //查询2
        GridView1.DataSource = from p in dc.Person
                               where p.ID < 100 && p.Name.StartsWith("bei")
                               select new { 编码 = p.ID, 姓名 = p.Name, 电话 = p.Tel };
        //查询3
        var varPerson = from p in dc.Person
                         where p.ID < 100 && p.Tel.StartsWith("010")
                         orderby p.Name descending
                         select new { 编码 = p.ID, 姓名 = p.Name, 电话 = p.Tel };
        GridView1.DataSource = varPerson;

        //添加
        Person person1 = new Person();
        person1.Name = "xianggang";
        person1.Tel = "00852-11111111";
        dc.Person.InsertOnSubmit(person1);
        dc.SubmitChanges();

        //更新
        Person person2 = dc.Person.Where(p => p.Name == "xianggang").First();
        person2.Tel = "00852-11111110";
        dc.SubmitChanges();


        //删除
        Person person3 = dc.Person.Where(p => p.Name == "xianggang").First();
        dc.Person.DeleteOnSubmit(person3);
        dc.SubmitChanges();

        //查询1
        GridView1.DataSource = dc.Person;
        GridView1.DataBind();
    }

=>
     返回结果:

IDNameTel
1beijing010-0000000
2shanghai021-0000000
13xianggang00852-11111111
14xianggang00852-11111111
15xianggang00852-11111111
16xianggang00852-11111111

=>

=>

1、创建linq to sql(即DataPerson.dbml文件):

#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行库版本:2.0.50727.1433
//
//     对此文件的更改可能会导致不正确的行为,并且如果
//     重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

[System.Data.Linq.Mapping.DatabaseAttribute(Name="msdb")]
public partial class DataPersonDataContext : System.Data.Linq.DataContext
{

private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();

#region Extensibility Method Definitions
partial void OnCreated();
partial void InsertPerson(Person instance);
partial void UpdatePerson(Person instance);
partial void DeletePerson(Person instance);
#endregion

public DataPersonDataContext() :
    base(global::System.Configuration.ConfigurationManager.ConnectionStrings["msdbConnectionString"].ConnectionString, mappingSource)
{
   OnCreated();
}

public DataPersonDataContext(string connection) :
    base(connection, mappingSource)
{
   OnCreated();
}

public DataPersonDataContext(System.Data.IDbConnection connection) :
    base(connection, mappingSource)
{
   OnCreated();
}

public DataPersonDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
    base(connection, mappingSource)
{
   OnCreated();
}

public DataPersonDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
    base(connection, mappingSource)
{
   OnCreated();
}

public System.Data.Linq.Table<Person> Person
{
   get
   {
    return this.GetTable<Person>();
   }
}
}

[Table(Name="dbo.Person")]
public partial class Person : INotifyPropertyChanging, INotifyPropertyChanged
{

private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

private int _ID;

private string _Name;

private string _Tel;

    #region Extensibility Method Definitions
    partial void OnLoaded();
    partial void OnValidate(System.Data.Linq.ChangeAction action);
    partial void OnCreated();
    partial void OnIDChanging(int value);
    partial void OnIDChanged();
    partial void OnNameChanging(string value);
    partial void OnNameChanged();
    partial void OnTelChanging(string value);
    partial void OnTelChanged();
    #endregion

public Person()
{
   OnCreated();
}

[Column(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int ID
{
   get
   {
    return this._ID;
   }
   set
   {
    if ((this._ID != value))
    {
     this.OnIDChanging(value);
     this.SendPropertyChanging();
     this._ID = value;
     this.SendPropertyChanged("ID");
     this.OnIDChanged();
    }
   }
}

[Column(Storage="_Name", DbType="NChar(10)")]
public string Name
{
   get
   {
    return this._Name;
   }
   set
   {
    if ((this._Name != value))
    {
     this.OnNameChanging(value);
     this.SendPropertyChanging();
     this._Name = value;
     this.SendPropertyChanged("Name");
     this.OnNameChanged();
    }
   }
}

[Column(Storage="_Tel", DbType="NChar(20)")]
public string Tel
{
   get
   {
    return this._Tel;
   }
   set
   {
    if ((this._Tel != value))
    {
     this.OnTelChanging(value);
     this.SendPropertyChanging();
     this._Tel = value;
     this.SendPropertyChanged("Tel");
     this.OnTelChanged();
    }
   }
}

public event PropertyChangingEventHandler PropertyChanging;

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void SendPropertyChanging()
{
   if ((this.PropertyChanging != null))
   {
    this.PropertyChanging(this, emptyChangingEventArgs);
   }
}

protected virtual void SendPropertyChanged(String propertyName)
{
   if ((this.PropertyChanged != null))
   {
    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
   }
}
}
#pragma warning restore 1591


3.linq多表查询


sql语句

select t_friend.userid,friendid,truename,formid

from dbo.t_friend left join dbo.t_userinfo

on dbo.t_friend.friendid=dbo.t_userinfo.userid

where dbo.t_friend.userid=5 and applystate=1

linq语句

public void bind()

        {

            blogdatabaseDataContext dc = new blogdatabaseDataContext();

            Table<t_userinfo> login = dc.GetTable<t_userinfo>();

            Table<t_friend> t_f=dc.GetTable<t_friend>();

            var info = from t in  t_f

                   join c in login on  t.friendid equals c.userid

                       where(  t.userid == 5 && t.applystate == true)

                       select new

                       {                          

                           t.formid,

                           t.userid,

                           t.friendid,                         

                           c.truename,

                           c.picture 

                       };

 

            this.GridView1.DataSource = info;

            this.GridView1.DataBind();

        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值