要使用LINQ to SQL要完成以下几个步骤
1、先将数据库的表映射一个实体类

 1 [Table(Name = "Customers")]
 2 class Contact
 3 {
 4     [Column(IsPrimaryKey=true)]
 5     public string CustomerID { setget; }
 6     [Column(Name = "ContactName")]
 7     public string Name { setget; }
 8     [Column]
 9     public string City { setget; }
10 }
2、实例化 DataContextDataContext主要是建立SQL与数据库之间的映射
string connect = "server=.;database=Northwind;uid=sa;pwd=";
DataContext db 
= new DataContext(connect);

3、和其他一样了,查询和显示
var contacts = from contact in db.GetTable<Contact>()
               
where contact.City == "London"
               select contact;
foreach (var contact in contacts)
{
    Console.WriteLine(contact.Name);
}

相关文章:

  • 2021-09-12
  • 2021-12-14
  • 2022-12-23
  • 2022-12-23
  • 2021-07-10
  • 2022-12-23
  • 2022-02-14
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-05
  • 2021-12-13
  • 2021-12-22
  • 2022-12-23
相关资源
相似解决方案