블로그 이미지
따시쿵

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

2015. 7. 6. 11:54 C# with LINQ to SQL

이번 장에서는 스토어드 프로시져를 호출하는 방법과 호출된 sql 문을 확인 해 보도록 하겠습니다.

 

1. 스토어드 프로시서를 생성합니다.

CREATE PROCEDURE dbo.GetCustomers
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;


	select CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax
	from [dbo].[Customers]
END
GO

 

2. [서버 탐색기]를 펼쳐서 위에서 생성한 프로시저를 찾아서 오른쪽 윈도우창에 드래그&드랍 시킵니다.

 

3. 프로그램 쿼리문을 스토어 프로시저를 호출하는 구문으로 변경합니다.

        #region Select ALL
        private void GetData()
        {
            northwindDataContext dbContext = new northwindDataContext();
            dataGridView1.DataSource = dbContext.GetCustomers();    // 스토어 프로시저 호출하는 구문
        }
        #endregion

 

 

4. [SQL Server Management Studio] 에서 [도구]창에서 [SQL Profiler]를 실행 한 후, 프로그램을 실행해서 생성된 sql 문을 확인 해 봅니다. 

 

 

5. 스토어드 프로시저가 프로그램에 포함되는 형태입니다.

 

실제로 프로시저가 northwind.designer.cs 파일에 아래와 같이 포함되어 있습니다.

		[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.GetCustomers")]
		public ISingleResult<Customers> GetCustomers()
		{
			IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
			return ((ISingleResult<Customers>)(result.ReturnValue));
		}

 

 

전체 소스

MyLINQ9.zip

 

posted by 따시쿵