2014-04-15 5 views
0

원본 MS Access 데이터베이스에 MyTable1이라는 테이블이 있다고 가정 해 봅니다. MyTable1에 복합 기본 키가 있음 (두 개의 개별 필드 Phone 및 City 필드의 조합)이 있다고 가정 해 보겠습니다. 이제 다음 코드를 사용하여 복사 할 때 City를 대상 테이블의 복합 키의 일부로 만들지 않습니다.어떻게 프로그래밍 방식으로 복합 키가있는 MS Access 테이블 스키마를 복사합니까?

  ADOX.Table sourceTable = default(ADOX.Table); 
      sourceTable = sourceCat.Tables[tableName.Trim()]; 

      ADOX.Table newTable = new ADOX.Table(); 
      newTable.ParentCatalog = targetCat; 

      tempNewtableName = sourceCat.Tables[tableName.Trim()].Name; 
      newTable.Name = tempNewtableName; 

      ADOX.Column newCol = default(ADOX.Column); 
      DataTable primaryKeyDT = new DataTable(); 
      primaryKeyDT.Columns.Add("FieldName"); 
      primaryKeyDT.Columns.Add("Type"); 

      foreach (ADOX.Index idx1 in sourceCat.Tables[tableName].Indexes) 
      { 
       if (idx1.PrimaryKey == true) 
       { 
        primaryKeyDT.Rows.Add(idx1.Columns[0].Name, idx1.Name); 
       } 
      } 


      foreach (ADOX.Column SourceCol in sourceTable.Columns) 
      { 
       newCol = new ADOX.Column(); 

       newCol.Type = SourceCol.Type; 
       newCol.DefinedSize = SourceCol.DefinedSize; 
       newCol.ParentCatalog = targetCat; 
       newCol.Precision = SourceCol.Precision; 

       newCol.DefinedSize = SourceCol.DefinedSize; 
       newCol.Attributes = SourceCol.Attributes; 
       newCol.Name = SourceCol.Name; 

       newCol.NumericScale = SourceCol.NumericScale; 

       newTable.Columns.Append(newCol); 

       DataRow[] results = primaryKeyDT.Select("FieldName ='" + SourceCol.Name + "'"); 
       if (results.Length > 0) 
       { 
        idx = new Index(); 
        idx.Name = "idx_" + SourceCol.Name; 
        idx.PrimaryKey = true; 
        idx.Columns.Append(SourceCol.Name); 

        newTable.Indexes.Append(idx); 
       } 

      } 

      targetCat.Tables.Append(newTable); 

답변

0

를 해결하려면 어떻게 당신은 결과 []를 반복하고 idx.Columns 모음으로 각 필드를 추가해야합니다