#pragma warning disable 1591namespace _11.LINQtoSQL.Web
{
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;
问题一:“VS2010”中的“LINQtoSQL类”文件,有什么优势吗?是不是可以不用写“增、删、改、查”这些SQL语句,不用SQL语句???增、删、改、查又是怎样实现的???  [global::System.Data.Linq.Mapping.DatabaseAttribute(Name="NORTHWND")]
问题二:这句话的作用是什么呢?

    
public partial class DataClasses1DataContext : System.Data.Linq.DataContext
{private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
问题三:这句话的作用是什么?“AttributeMappingSource”指什么嘞,数据库映射吗?

 
  #region 可扩展性方法定义
  partial void OnCreated();  partial void InsertProduct(Product instance);
  partial void UpdateProduct(Product instance);
  partial void DeleteProduct(Product instance);  问题三:“OnCreated()”是自动创建的,“InsertProduct”、“UpdateProduct”、“DeleteProduct”,是后来手动加上的把,就像“.cs”类中的一样?只有声明也没有具体的实现方法啊?  #endregionpublic DataClasses1DataContext() :  
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ConnectionString, mappingSource)
{
OnCreated();
}public DataClasses1DataContext(string connection) :  
base(connection, mappingSource)
{
OnCreated();
}public DataClasses1DataContext(System.Data.IDbConnection connection) :  
base(connection, mappingSource)
{
OnCreated();
}public DataClasses1DataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :  
base(connection, mappingSource)
{
OnCreated();
}public DataClasses1DataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :  
base(connection, mappingSource)
{
OnCreated();
}
问题四:这几个“DataClasses1DataContext”是建立数据库连接的不同的方法,对吧? public System.Data.Linq.Table<Product> Products
{
get
{
return this.GetTable<Product>();
}
}
}[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Products")]
public partial class Product : INotifyPropertyChanging, INotifyPropertyChanged
问题五:这个类要被Sivlight使用所以,继承了“INotifyPropertyChanging, INotifyPropertyChanged”接口对吧???
问题六:“类”使用了“global::System.Data.Linq.Mapping.TableAttribute”,“属性”使用了“global::System.Data.Linq.Mapping.ColumnAttribute”,是不是将“Linq”
中的“Table”,转化为“类”中的属性了?
{private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);private int _ProductID;private string _ProductName;private System.Nullable<int> _SupplierID;private System.Nullable<int> _CategoryID;private string _QuantityPerUnit;private System.Nullable<decimal> _UnitPrice;private System.Nullable<short> _UnitsInStock;private System.Nullable<short> _UnitsOnOrder;private System.Nullable<short> _ReorderLevel;private bool _Discontinued;  #region 可扩展性方法定义
  partial void OnLoaded();
  partial void OnValidate(System.Data.Linq.ChangeAction action);
  partial void OnCreated();
  partial void OnProductIDChanging(int value);
  partial void OnProductIDChanged();
  partial void OnProductNameChanging(string value);
  partial void OnProductNameChanged();
  partial void OnSupplierIDChanging(System.Nullable<int> value);
  partial void OnSupplierIDChanged();
  partial void OnCategoryIDChanging(System.Nullable<int> value);
  partial void OnCategoryIDChanged();
  partial void OnQuantityPerUnitChanging(string value);
  partial void OnQuantityPerUnitChanged();
  partial void OnUnitPriceChanging(System.Nullable<decimal> value);
  partial void OnUnitPriceChanged();
  partial void OnUnitsInStockChanging(System.Nullable<short> value);
  partial void OnUnitsInStockChanged();
  partial void OnUnitsOnOrderChanging(System.Nullable<short> value);
  partial void OnUnitsOnOrderChanged();
  partial void OnReorderLevelChanging(System.Nullable<short> value);
  partial void OnReorderLevelChanged();
  partial void OnDiscontinuedChanging(bool value);
  partial void OnDiscontinuedChanged();
  #endregionpublic Product()
{
OnCreated();
}[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ProductID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int ProductID
{
get
{
return this._ProductID;
}
set
{
if ((this._ProductID != value))
{
this.OnProductIDChanging(value);
this.SendPropertyChanging();
this._ProductID = value;
this.SendPropertyChanged("ProductID");
this.OnProductIDChanged();
}
}
}[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ProductName", DbType="NVarChar(40) NOT NULL", CanBeNull=false)]
public string ProductName
{
get
{
return this._ProductName;
}
set
{
if ((this._ProductName != value))
{
this.OnProductNameChanging(value);
this.SendPropertyChanging();
this._ProductName = value;
this.SendPropertyChanged("ProductName");
this.OnProductNameChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Discontinued", DbType="Bit NOT NULL")]
public bool Discontinued
{
get
{
return this._Discontinued;
}
set
{
if ((this._Discontinued != value))
{
this.OnDiscontinuedChanging(value);
this.SendPropertyChanging();
this._Discontinued = value;
this.SendPropertyChanged("Discontinued");
this.OnDiscontinuedChanged();
}
}
}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));
}
}问题六:“PropertyChanging”和“PropertyChanged”继承自“INotifyPropertyChanging”、 “INotifyPropertyChanged”这两个接口,为了实现Silverlight中数据的更新对吧?
}
}
#pragma warning restore 1591

解决方案 »

  1.   

    问题一:“VS2010”中的“LINQtoSQL类”文件,有什么优势吗?是不是可以不用写“增、删、改、查”这些SQL语句,不用SQL语句???增、删、改、查又是怎样实现的???
    目前LinqToSQL被Entity Framework取代。如何实现你看代码就知道了。是否有优势这个不是绝对的。“飞机比火车有优势么”当然飞机更快,但是票价贵,不准点。“火车比飞机有优势么”,当然火车便宜、安全,但是不够快,是不是?[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="NORTHWND")]
    问题二:这句话的作用是什么呢?
    表示映射的数据库名
        
    private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
    问题三:这句话的作用是什么?“AttributeMappingSource”指什么嘞,数据库映射吗?
    表示映射数据源
        问题三:“OnCreated()”是自动创建的,“InsertProduct”、“UpdateProduct”、“DeleteProduct”,是后来手动加上的把,就像“.cs”类中的一样?只有声明也没有具体的实现方法啊?
    是VS自动生成的代码问题四:这几个“DataClasses1DataContext”是建立数据库连接的不同的方法,对吧?
    这个,你的理解差距太大了。DataContext关联给一个映射数据源的数据库上下文类。问题五:这个类要被Sivlight使用所以,继承了“INotifyPropertyChanging, INotifyPropertyChanged”接口对吧???
    和Silverlight没有关系,表示在属性修改的时候会通知调用者。问题六:“类”使用了“global::System.Data.Linq.Mapping.TableAttribute”,“属性”使用了“global::System.Data.Linq.Mapping.ColumnAttribute”,是不是将“Linq”
    中的“Table”,转化为“类”中的属性了?
    可以这么理解吧,当然幕后,有一系列代码在做这件事。
      

  2.   

    你的问题主要有2大方面,一个是对C#语法不太会,像Attribute、类、接口这些都是一知半解的。另一个是对Linq To SQL没有什么概念,也分不清LinqToSQL和Visual Studio生成代码之间的关系。建议你先学习基本的C#语法,以及Visual Studio的操作。
      

  3.   

    问题一:“VS2010”中的“LINQtoSQL类”文件,有什么优势吗?是不是可以不用写“增、删、改、查”这些SQL语句,不用SQL语句???增、删、改、查又是怎样实现的???你先了解一下ORM,然后再弄明白linq 中的 Provider[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="NORTHWND")]
    问题二:这句话的作用是什么呢?Attribute   简单的说就是指定了映射的数据库名
        private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
    问题三:这句话的作用是什么?“AttributeMappingSource”指什么嘞,数据库映射吗?是指以Attribute的方式来映射
    问题四:这几个“DataClasses1DataContext”是建立数据库连接的不同的方法,对吧?重载方法[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Products")]
    public partial class Product : INotifyPropertyChanging, INotifyPropertyChanged
    问题五:这个类要被Sivlight使用所以,继承了“INotifyPropertyChanging, INotifyPropertyChanged”接口对吧???和silverlight没关系,是要获知属性是否变更的一种方式
    问题六:“类”使用了“global::System.Data.Linq.Mapping.TableAttribute”,“属性”使用了“global::System.Data.Linq.Mapping.ColumnAttribute”,是不是将“Linq”
    中的“Table”,转化为“类”中的属性了?映射
    问题六:“PropertyChanging”和“PropertyChanged”继承自“INotifyPropertyChanging”、 “INotifyPropertyChanged”这两个接口,为了实现Silverlight中数据的更新对吧?见问题五
      

  4.   

    小弟的理解:
    简单的说“SQLToLinq类”和“ADO.Net”的功能是一样的,都是实现数据的访问。只是“SQLToLinq类”是通过的“数据库映射”来实现的,而“ADO.Net”是通过“SqlConnect”、“SqlCommand”这些类来实现的。“Linq”可是现实“ORM”,“ADO.Net”可以实现分层。小弟的理解对吗???
      

  5.   

    (1)Linq To SQL 应该是建立在 ADO.NET 基础上的。所以 Linq To SQL 能做的事情,ADO.NET 都能做。
    (2)Linq != Linq To SQL,前者是语法元素,后者主要是指 Linq To SQL Provider,一个库。
    (3)Linq To SQL可以实现ORM,但是ADO.NET也可以,实际上Linq To SQL就是建立在ADO.NET上的,换一句话说,Linq To SQL本身能做的事情,你也可以做到(不过相当复杂),另外,像 NHibernate 之类的 ORM 框架就是完全基于 ADO.NET 的。
    (4)分层是软件架构问题。实际上我们把程序分为表示层、业务逻辑和数据访问层,而LINQ To SQL和ADO.NET都可以作为数据访问层的一种实现。
    (5)Linq可以和ADO.NET共同使用,因为Linq单单就是语法而已。希望你不要从简单的回答和你的想象去理解,而是踏踏实实地学一下。
      

  6.   

    本帖最后由 caozhy 于 2012-01-14 15:45:20 编辑
      

  7.   

    我也在学linq,一起加油哦,
    linq是.net3.5才开始有的,没有.net环境linq就不能用.