2009-09-07 63 views
0

我想在LINQ中構建我的hello-world程序。LINQ入門問題

在執行以下代碼:

(這是我的LINQ到由VS2008生成的SQL類。)

#pragma warning disable 1591 
//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated by a tool. 
//  Runtime Version:2.0.50727.3053 
// 
//  Changes to this file may cause incorrect behavior and will be lost if 
//  the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

namespace LINQ_to_SQL_Test 
{ 
    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; 


    [System.Data.Linq.Mapping.DatabaseAttribute(Name="LINQ_Test")] 
    public partial class PersonDataContext : 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 PersonDataContext() : 
       base(global::LINQ_to_SQL_Test.Properties.Settings.Default.LINQ_TestConnectionString, mappingSource) 
     { 
      OnCreated(); 
     } 

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

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

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

     public PersonDataContext(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>(); 
      } 
     } 
    } 

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

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

     private int _ID; 

     private System.Nullable<int> _IDRole; 

     private string _LastName; 

     private string _FirstName; 

    #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 OnIDRoleChanging(System.Nullable<int> value); 
    partial void OnIDRoleChanged(); 
    partial void OnLastNameChanging(string value); 
    partial void OnLastNameChanged(); 
    partial void OnFirstNameChanging(string value); 
    partial void OnFirstNameChanged(); 
    #endregion 

     public Person() 
     { 
      OnCreated(); 
     } 

     [Column(Storage="_ID", DbType="Int NOT NULL", IsPrimaryKey=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="_IDRole", DbType="Int")] 
     public System.Nullable<int> IDRole 
     { 
      get 
      { 
       return this._IDRole; 
      } 
      set 
      { 
       if ((this._IDRole != value)) 
       { 
        this.OnIDRoleChanging(value); 
        this.SendPropertyChanging(); 
        this._IDRole = value; 
        this.SendPropertyChanged("IDRole"); 
        this.OnIDRoleChanged(); 
       } 
      } 
     } 

     [Column(Storage="_LastName", DbType="VarChar(50)")] 
     public string LastName 
     { 
      get 
      { 
       return this._LastName; 
      } 
      set 
      { 
       if ((this._LastName != value)) 
       { 
        this.OnLastNameChanging(value); 
        this.SendPropertyChanging(); 
        this._LastName = value; 
        this.SendPropertyChanged("LastName"); 
        this.OnLastNameChanged(); 
       } 
      } 
     } 

     [Column(Storage="_FirstName", DbType="VarChar(50)")] 
     public string FirstName 
     { 
      get 
      { 
       return this._FirstName; 
      } 
      set 
      { 
       if ((this._FirstName != value)) 
       { 
        this.OnFirstNameChanging(value); 
        this.SendPropertyChanging(); 
        this._FirstName = value; 
        this.SendPropertyChanged("FirstName"); 
        this.OnFirstNameChanged(); 
       } 
      } 
     } 

     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 

這是我的手工編碼的驅動器程序:

class Program 
    { 
     static void Main(string[] args) 
     { 
      PersonDataContext dc = new PersonDataContext("LINQ_TestConnectionString"); 

      Person person = new Person(); 
      person.ID = 4; 
      person.IDRole = 1; 
      person.FirstName = "aaa"; 
      person.LastName = "bbb"; 

      dc.Persons.InsertOnSubmit(person); 
      dc.SubmitChanges(); 
     } 
    } 

我從SQL Server收到以下錯誤消息。

A network-related or instance-specific error occurred while 
establishing a connection to SQL Server. The server was not found 
or was not accessible. Verify that the instance name is correct 
and that SQL Server is configured to allow remote connections. 
(provider: Named Pipes Provider, error: 40 - Could not open a 
connection to SQL Server) 

我該如何解決問題?

回答

3

你正在傳遞字符串文字"LINQ_TestConnectionString"到構造函數 - 所以它把它當作連接字符串本身。這不是連接字符串,它只是連接字符串的設置的名稱。

的參數的構造函數 - 將自動使用LINQ_TestConnectionString設置:

PersonDataContext dc = new PersonDataContext(); 

另外,通過一個真正連接的字符串中。

1

既然你draged並從數據庫中刪除的表的連接字符串已經存在,只使用默認constractor:如果你想傳遞一個連接字符串使用這個

new PersonDataContext(); 

Data Source=ComputerName\Instancename;Initial Catalog=DataBaseName;Persist Security Info=True;