2015. 7. 15. 11:23
C# with LINQ to SQL
SqlMetal 은 LINQ-to-SQL class 를 만드는 command-line 코드 생성기 툴입니다. 실행 파일입니다.
LINQ-to-SQL class 를 만드는 두 가지 방법은 아래와 같습니다.
- Visual Studio
- SqlMetal command-line code generator
1. SqlMetal 파일 위치 입니다.
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin
2. 관리자 권한으로 Visual Studio Command Prompt 를 실행 시킵니다.
3. 새로 생성될 dbml 파일을 저장할 폴더를 하나 만듭니다.
D:\win_project\winformapp4\SqlMetalFile
4. sqlmetal 을 이용해서 dbml 파일을 만듭니다.
예제는 지금까지 사용한 northwind database 를 dbml 파일로 만드는 과정을 보여 줍니다.
SqlMetal 에 대한 자세한 정보는 아래 사이트를 참조하시기 바랍니다.
https://msdn.microsoft.com/ko-kr/library/bb386987(v=vs.110).aspx
5. Visual Studio 를 실행하고 windows form 으로 프로젝트를 만들고, 화면에 dataGridView control 를 추가합니다.
northwind 디비에 있는 employees 테이블을 모두 가져와서 화면에 보여 주는 작업을 할 것입니다.
6. app.Config 파일에 northwind 디비를 연결하는 connection string 을 설정합니다.
1 2 3 4 5 6 7 8 9 | <!--?xml version="1.0" encoding="utf-8" ?--> < configuration > < connectionstrings > < add name = "NorthwindConnectionString" connectionstring = "Data Source=HAPPY;Initial Catalog=northwind;Persist Security Info=True;User ID=sa;Password=*********" providername = "System.Data.SqlClient" > </ add ></ connectionstrings > < startup > < supportedruntime version = "v4.0" sku = ".NETFramework,Version=v4.5" > </ supportedruntime ></ startup > </ configuration > |
7. 4번 단계에서 생성한 dbml 파일을 [프로젝트] => [추가] => [기존 항목]으로 프로젝트에 import 시킵니다.
8. 폼 로드시에 employees 테이블을 가져와서 화면에 보여 줍니다.
1 2 3 4 5 6 7 8 9 | private void Form1_Load( object sender, EventArgs e) { string connectinstring = ConfigurationManager.ConnectionStrings[ "NorthwindConnectionString" ].ConnectionString; using (NorthwindDataContext dbConext = new NorthwindDataContext(connectinstring)) { dataGridView1.DataSource = dbConext.Employees; } } |
소스 파일
'C# with LINQ to SQL' 카테고리의 다른 글
Eager loading (0) | 2015.08.10 |
---|---|
Lazy loading (0) | 2015.08.07 |
LINQ - out 파라미터를 이용한 스토어드 프로시저 이용 (0) | 2015.07.09 |
LINQ - 스토어드 프로시저를 이용한 insert, update, delete 연산 (0) | 2015.07.07 |
LINQ - 스토어드 프로시저 호출 (select) (0) | 2015.07.06 |