2012-02-11 4 views
0

나는 부모 클래스 (Product)와 그것으로부터 상속받은 두 개의 자식 클래스 (Book, Software)를 가지고있다. 공통 데이터 (상위 클래스 속성)를 Products 테이블에 삽입하고 하위 클래스의 특정 속성을 BookProducts 및 SoftwareProducts 테이블에 삽입해야합니다.자식 개체의 특성을 데이터베이스에 올바르게 삽입하는 방법은 무엇입니까?

이것은 내가 구현 한 구현입니다. 누군가가 더 나은 접근 방식을 제공 할 수 있는지 궁금합니다.

public static class Database 
{ 


    public static void SaveBook(Book book) 
    { 
     //save the product to products table and return the ID of the new row 
     int ProductID = saveProduct(book); 

     SqlConnection connection = DatabaseConnection.GetConnection(); 


      string insertBookStatement = 
      "INSERT BookProducts" + 
      "(ProductID,Author)" + 
      "VALUES (@ProductID,@Author)"; 

     SqlCommand insertBookCommand = new SqlCommand(insertBookStatement, connection); 

     insertBookCommand.Parameters.AddWithValue("@ProductID",ProductID); 
     insertBookCommand.Parameters.AddWithValue("@Author",book.Author); 


     try 
     { 
      connection.Open(); 
      insertBookCommand.ExecuteNonQuery(); 

     } 
     catch (SqlException ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      connection.Close(); 
     } 



    } 

    public static void SaveSoftware(Software software) 

    { 
     int ProductID = saveProduct(software); 

     SqlConnection connection = DatabaseConnection.GetConnection(); 


     string insertSoftwareStatement = 
     "INSERT SoftwareProducts" + 
     "(ProductID,VersionNumber)" + 
     "VALUES (@ProductID,@VersionNumber)"; 

     SqlCommand insertSoftwareCommand = new SqlCommand(insertSoftwareStatement, connection); 

     insertSoftwareCommand.Parameters.AddWithValue("@ProductID", ProductID); 
     insertSoftwareCommand.Parameters.AddWithValue("@VersionNumber", software.VersionNumber); 


     try 
     { 
      connection.Open(); 
      insertSoftwareCommand.ExecuteNonQuery(); 

     } 
     catch (SqlException ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      connection.Close(); 
     } 
    } 


    private static int saveProduct(Product p) 
    { 
     int ProductID; 
     SqlConnection connection = DatabaseConnection.GetConnection(); 

     string insertProductStatement = 
      "INSERT Products" + 
      "(Code,Description,Category,Price)" + 
      "VALUES (@code,@Description,@Category,@Price)"; 

     SqlCommand insertProductCommand = new SqlCommand(insertProductStatement, connection); 
     insertProductCommand.Parameters.AddWithValue("@Code", p.Code); 
     insertProductCommand.Parameters.AddWithValue("@Description", p.ShortDescription); 
     insertProductCommand.Parameters.AddWithValue("@Category", p.Category); 
     insertProductCommand.Parameters.AddWithValue("@Price", p.Price); 

     try 
     { 
      connection.Open(); 
      insertProductCommand.ExecuteNonQuery(); 
      string selectStatement = "SELECT IDENT_CURRENT('Products') FROM Products"; 
      SqlCommand selectCommand = new SqlCommand(selectStatement, connection); 
      ProductID = Convert.ToInt16(selectCommand.ExecuteScalar()); 



     } 
     catch (SqlException ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      connection.Close(); 
     } 


     return ProductID; 
    } 


} 
+0

XML 형식을 저장 프로 시저에 전달할 수 있습니다. SP에서는 XML 데이터를 테이블 구조로 읽을 수 있습니다. http://msdn.microsoft.com/en-us/library/ms345117%28v=sql.90%29.aspx –

+0

더 나은 방법은 Entity Framework를 사용하는 것입니다. –

+0

EF는 그의 문제로 OP를 도우 려하지 않습니다. 이 솔루션은 모두 데이터 모델링 및 소프트웨어 디자인에 관한 것이므로 특정 ORM 기술을 선택해도 실제로 도움이되지는 않습니다. – Tobias

답변

1

데이터 액세스 개체 패턴을 사용하면 도움이됩니다.

우선, 하나의 BaseProduct 테이블, 하나의 소프트웨어 테이블 및 하나의 Book 테이블이 필요합니다. SoftwareProduct DAO는 base.Store()를 호출 할 것입니다. (이것은 꽤 가짜 코디이지만 전반적인 아이디어를 얻을 수 있습니다) BaseProduct에 모든 기본 클래스의 필드를 저장하고 BaseProductId 외래 키로.

데이터베이스 정규화가 일반적으로 좋으며 도메인 계층의 OCP/SRP와 데이터베이스의 정규화 된 테이블 간의 연결을 보여주기 때문에이 특별한 경우는 텍스트 북 예제로 사용할 수 있습니다.

관련 문제