借助LINQ to SQL,我们可以便捷的与数据库进行交互,本文演示了如何在.NET开发环境中使用LINQ to SQL
使用过程:
1.在数据库中创好一张表
2.在VS环境中链接到数据库
3.在工程中创建一个新的L2S文件
4.将步骤2中的表,“拖入”DBML文件中的layout文件,如图,确认拖入的表即为我们刚创建的之后,保存
5.VS自动生designer.cs成了如下代码
#pragma warning disable 1591 //------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // Runtime Version:4.0.30319.34014 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace LinqToDB { using System.Data.Linq; using System.Data.Linq.Mapping; using System.Data; using System.Collections.Generic; using System.Reflection; using System.Linq; using System.Linq.Expressions; using System.ComponentModel; using System; [global::System.Data.Linq.Mapping.DatabaseAttribute(Name="ClientInfo")] public partial class ClientInfoDataContext : 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 ClientInfoDataContext() : base(global::LinqToDB.Properties.Settings.Default.ClientInfoConnectionString, mappingSource) { OnCreated(); } public ClientInfoDataContext(string connection) : base(connection, mappingSource) { OnCreated(); } public ClientInfoDataContext(System.Data.IDbConnection connection) : base(connection, mappingSource) { OnCreated(); } public ClientInfoDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : base(connection, mappingSource) { OnCreated(); } public ClientInfoDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : base(connection, mappingSource) { OnCreated(); } public System.Data.Linq.Table<Person> Persons { get { return this.GetTable<Person>(); } } } [global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Person")] public partial class Person : INotifyPropertyChanging, INotifyPropertyChanged { private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); private string _ID; private string _Name; private string _Date; #region Extensibility Method Definitions partial void OnLoaded(); partial void OnValidate(System.Data.Linq.ChangeAction action); partial void OnCreated(); partial void OnIDChanging(string value); partial void OnIDChanged(); partial void OnNameChanging(string value); partial void OnNameChanged(); partial void OnDateChanging(string value); partial void OnDateChanged(); #endregion public Person() { OnCreated(); } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ID", DbType="VarChar(50) NOT NULL", CanBeNull=false, IsPrimaryKey=true)] public string ID { get { return this._ID; } set { if ((this._ID != value)) { this.OnIDChanging(value); this.SendPropertyChanging(); this._ID = value; this.SendPropertyChanged("ID"); this.OnIDChanged(); } } } [global::System.Data.Linq.Mapping.ColumnAttribute(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(); } } } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Date", DbType="VarChar(50)")] public string Date { get { return this._Date; } set { if ((this._Date != value)) { this.OnDateChanging(value); this.SendPropertyChanging(); this._Date = value; this.SendPropertyChanged("Date"); this.OnDateChanged(); } } } 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