2013년 8월 30일 금요일

Execute CRUD-LINQ

ExecuteCommand : 요청할 쿼리문과 쿼리문에 적용할 파라미터를 전달받아 실행하고 실행 결과를 int 타입으로 리턴한다.
                                이 값은 쿼리문이 실행된 이후에 영향받은 데이터 건수를 가진다.

Select


IEnumerable<Products> result = north.ExecuteQuery<Products>
            ("Select * from products where unitprice >= {0}", 100);
foreach(Products prod in result)
{
    Console.WriteLIne("ProductID : {0}, Unit Price : {1}", 
                      prod.ProductID, 
                      prod.UnitPrice);
}

Insert, Update, Delete

public void ExecuteCommandTest()
{
    const string insertProd = "insert into products values({0}, {1}, {3}, {4}, {5})";
    const string updateProd = "update products set UnitPrice = UnitPrice + 2.00 where UnitPrice >= {0}";
    const string deleteProd = "delete products where unitprice >= {0}";
 
    try
    {
        int insertAffectedCnt = north.ExecuteCommand(insertProd, "Test Prod",
                                                     10,         1,
                                                     "31 boxes", 7.00,
                                                     61);
        int updateAffectedCnt = north.ExecuteCommand(updateProd, 100);
        int deleteAffectedCnt = north.ExecuteCommand(deleteProd, 100);
  
        //변경 사항을 데이터베이스에 반영
        north.SubmitChanges();
 
        //추가, 수정, 삭제 처리 후 영향받은 데이터 건수 체크
        Console.WriteLine("추가된 건 수 : {0} 수정된 건 수 : {1} 삭제된 건 수 : {2}",
                      insertAffectedCnt, updateAffectedCnt, deleteAffectedCnt);
    }
    catch(Exception ex){ Console.WriteLine(ex.Message); }
}

댓글 없음:

댓글 쓰기