LINQ to SQL
.NET프로그래밍/ADO.NET 2009. 9. 30. 17:18 |
* "윈폼"에서 '새 프로젝트 만들기'로 프로젝트 실행
==> Form1.cs [디자인]
-------------------------------------------------------------------------------------
==> AddressBook.cs 소스
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace WinLinqToSQL
{
/// <summary>
/// AddressBook 테이블과 일대일 매칭되는 클래스
/// </summary>
[Table(Name = "AddressBook")] // Table 특성을 사용해서 테이블로 보자...
public class AddressBook
{
private int _Num;
[Column(IsPrimaryKey = true, Storage = "_Num")]
public int Num // Num 속성은 Num 컬럼과 매칭, 기본키 설정
{
get { return _Num; }
set { _Num = value; }
}
private string _Name;
[Column(Storage = "_Name")]
public string Name
{
get { return _Name; }
set { _Name = value; }
}
private string _Mobile;
[Column(Storage = "_Mobile")]
public string Mobile
{
get { return _Mobile; }
set { _Mobile = value; }
}
private string _Email;
[Column(Storage = "_Email")]
public string Email
{
get { return _Email; }
set { _Email = value; }
}
}
}
-------------------------------------------------------------------------------------
==> Form1.cs 소스
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Linq;
namespace WinLinqToSQL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 어쨌든, 데이터 출력하자... 원하는 방법으로...
// 그렇지만, 이번에는 LINQ To SQL 방법으로
// Table과 동일한 구조의 Entity 클래스가 만들어졌다면...
//[1]
DataContext context = new DataContext(
"server=.;database=AddressBook;uid=AddressBook;pwd=6750440;");
//[2] context의 GetTable<T> 팩터리 메서드로 테이블 매핑해서 가져오기
Table<AddressBook> addr = context.GetTable<AddressBook>();
//[3] LINQ Query 날리기
var q = from a in addr
select a;
//[4] 출력
foreach (var item in q)
{
listBox1.Items.Add(item.Name + ", " + item.Mobile);
}
}
}
}
-------------------------------------------------------------------------------------
[실행결과]
'.NET프로그래밍 > ADO.NET' 카테고리의 다른 글
"마법사"(컨트롤기반)를 사용하여 ToDoList데이터베이스의 [dbo.Items]테이블의 데이터 가져오기 (0) | 2009.10.01 |
---|---|
ToDoList 프로그램(작업 목록) 만들기 (0) | 2009.10.01 |
LINQ to DataSet (0) | 2009.09.30 |
WinBinding(SqlDataAdapter클래스 내용에서 보충) (0) | 2009.09.30 |
(교재실습) WinCommandBuilder(SqlCommandBuilder 클래스) (0) | 2009.09.30 |