2014-03-31 3 views
1

master/detail 관계에 바인딩 된 2 개의 dataGridView가 있습니다. 두 번째 행을 마스터 dataGridView에 추가하려고하면 다음 오류가 발생합니다.마스터/세부 데이터 집합에 다중 행을 추가 할 때 제약 조건 오류

system.data.constraintexception 열 "Project Customer UBF Id"는 고유하게 제한됩니다. 값 ''이 (가) 이미 있습니다. 데이터 관계입니다.

자식 DataGridView에 여러 행을 추가 할 수 있으며 테이블간에 DataRelation을 제거하면 여러 행을 마스터에 추가 할 수 있습니다. 또한 다음 autoincrement 값으로 마스터의 기본 키를 수동으로 입력하면 여러 행을 추가 할 수있을뿐만 아니라 세부 정보를 다시 추가 할 수 있습니다.

테이블은 SQL 서버의 자동 증가 기본 키로 설정됩니다.

이 오류를 극복 어떤 도움을 자신의 업데이 트를 생성하여 해결

private void getData() 
{ 
    try 
    { 
     conn = new SqlConnection(connstr); 
     conn.Open(); 

     // Create a DataSet. 
     data = new DataSet(); 
     data.Locale = System.Globalization.CultureInfo.InvariantCulture; 

     string sqlStr = "SELECT [Project Customer UBF].* FROM [Project Customer UBF]; ";      

     // Add data from the Customers table to the DataSet. 
     masterDataAdapter = new 
      SqlDataAdapter(sqlStr, conn); 

     masterDataAdapter.Fill(data, "Customers"); 

     // Add data from the Orders table to the DataSet. 
     detailsDataAdapter = new 
      SqlDataAdapter("SELECT [Project Customer Discount].* FROM [Project Customer Discount]", conn); 
     detailsDataAdapter.Fill(data, "Discounts"); 

     // Establish a relationship between the two tables. 
     DataRelation relation = new DataRelation("CustDist", 
      data.Tables["Customers"].Columns["Project Customer UBF Id"], 
      data.Tables["Discounts"].Columns["Project Customer UBF Id"]); 
     data.Relations.Add(relation); 


     // Bind the master data connector to the Customers table. 
     masterBindingSource.DataSource = data; 
     masterBindingSource.DataMember = "Customers"; 
     masterBindingSource.Filter = "[Project Id] =" + _projectID; 

     // Bind the details data connector to the master data connector, 
     // using the DataRelation name to filter the information in the 
     // details table based on the current row in the master table. 
     detailsBindingSource.DataSource = masterBindingSource; 
     detailsBindingSource.DataMember = "CustDist"; 
     conn.Close();    
    } 
    catch (SqlException) 
    { 
     MessageBox.Show("To run this example, replace the value of the " + 
      "connectionString variable with a connection string that is " + 
      "valid for your system."); 
    } 
} 

private void ProjectEdit_Load(object sender, EventArgs e) 
{ 
    dataGridView1.DataSource = masterBindingSource; 
    dataGridView2.DataSource = detailsBindingSource; 
    getData(); 
} 

답변

0

을 감상 할 수있다, 삭제 및 마스터/부모 datatables 및 세부/자식 datatables에 대한 commannds을 추가

private void GenerateCommands() { 

    masterDataAdapter.InsertCommand = new SqlCommand(); 
    masterDataAdapter.InsertCommand.CommandText = "INSERT INTO [Project Customer UBF]([Project Id], [Customer Id]," + 
     "[Host Support], Warehoused,[List Price per Case]) " 
    + " VALUES (@ProjID, @CustID, 1, @Ware, 4); " 
    + " SELECT [Project Customer UBF Id],[Project Id], [Customer Id]," + 
     "[Host Support], Warehoused,[List Price per Case]" 
    + " FROM [Project Customer UBF] WHERE ([Project Customer UBF Id]= SCOPE_IDENTITY())"; 
    masterDataAdapter.InsertCommand.Connection = conn; 

    masterDataAdapter.InsertCommand.Parameters.Add("@ProjID", SqlDbType.Int,4,"Project Id"); 
    masterDataAdapter.InsertCommand.Parameters.Add("@CustID", SqlDbType.Int,4,"Customer Id"); 
    masterDataAdapter.InsertCommand.Parameters.Add("@Host", SqlDbType.Bit,1,"Host Support"); 
    masterDataAdapter.InsertCommand.Parameters.Add("@Ware", SqlDbType.Bit,1,"Warehoused"); 
    masterDataAdapter.InsertCommand.Parameters.Add("@List", SqlDbType.Float,8,"List Price per Case"); 


    masterDataAdapter.UpdateCommand = new SqlCommand(); 
    masterDataAdapter.UpdateCommand.CommandText = "UPDATE [Project Customer UBF] SET [Project Id] = @ProjID, " 
    + " [Customer Id] = @CustID, [Host Support] = @Host, Warehoused = @Ware, [List Price per Case] = @List " 
     + "WHERE ([Project Customer UBF Id] = @PCID); "; 

    masterDataAdapter.UpdateCommand.Connection = conn; 

    masterDataAdapter.UpdateCommand.Parameters.Add("@ProjID", SqlDbType.Int,4,"Project Id"); 
    masterDataAdapter.UpdateCommand.Parameters.Add("@CustID", SqlDbType.Int,4,"Customer Id"); 
    masterDataAdapter.UpdateCommand.Parameters.Add("@Host", SqlDbType.Bit,1,"Host Support"); 
    masterDataAdapter.UpdateCommand.Parameters.Add("@Ware", SqlDbType.Bit,1,"Warehoused"); 
    masterDataAdapter.UpdateCommand.Parameters.Add("@List", SqlDbType.Float,8,"List Price per Case"); 
    masterDataAdapter.UpdateCommand.Parameters.Add("@PCID", SqlDbType.Int,4,"Project Customer UBF Id"); 

    masterDataAdapter.DeleteCommand = new SqlCommand(); 
    masterDataAdapter.DeleteCommand.CommandText = "DELETE FROM [Project Customer UBF] " 
     + " WHERE ([Project Customer UBF Id] = @PCID);"; 

    masterDataAdapter.DeleteCommand.Connection = conn; 
    masterDataAdapter.DeleteCommand.Parameters.Add("@PCID", SqlDbType.Int,4,"Project Customer UBF Id"); 

    detailsDataAdapter.InsertCommand = new SqlCommand(); 
    detailsDataAdapter.InsertCommand.CommandText = "INSERT INTO [Project Customer Discount]([Project Customer UBF Id], " 
+ " [Discount Type], [Discount Amt], [Discount UOM]) " 
     + " VALUES (@PCID, @Type, @Amt, @UOM); " 
     + " SELECT [Discount Id],[Project Customer UBF Id], " 
+ " [Discount Type], [Discount Amt], [Discount UOM] " 
     + " FROM [Project Customer Discount] WHERE ([Discount Id] = SCOPE_IDENTITY())"; 
    detailsDataAdapter.InsertCommand.Connection = conn; 

    detailsDataAdapter.InsertCommand.Parameters.Add("@PCID", System.Data.SqlDbType.Int,4, "Project Customer UBF Id"); 
    detailsDataAdapter.InsertCommand.Parameters.Add("@Type", SqlDbType.Int,4,"Discount Type"); 
    detailsDataAdapter.InsertCommand.Parameters.Add("@Amt", SqlDbType.Float,8,"Discount Amt"); 
    detailsDataAdapter.InsertCommand.Parameters.Add("@UOM", SqlDbType.NVarChar,2,"Discount UOM"); 

    detailsDataAdapter.UpdateCommand = new SqlCommand(); 
    detailsDataAdapter.UpdateCommand.CommandText = "UPDATE [Project Customer Discount] SET [Project Customer UBF Id] = @PCID, " 
    + " [Discount Type] = @Type, [Discount Amt] = @Amt, [Discount UOM] = @UOM " 
     + "WHERE ([Discount Id] = @DID); "; 
    detailsDataAdapter.UpdateCommand.Connection = conn; 

    detailsDataAdapter.UpdateCommand.Parameters.Add("@PCID", System.Data.SqlDbType.Int, 4, "Project Customer UBF Id"); 
    detailsDataAdapter.UpdateCommand.Parameters.Add("@Type", SqlDbType.Int, 4, "Discount Type"); 
    detailsDataAdapter.UpdateCommand.Parameters.Add("@Amt", SqlDbType.Float, 8, "Discount Amt"); 
    detailsDataAdapter.UpdateCommand.Parameters.Add("@UOM", SqlDbType.NVarChar, 2, "Discount UOM"); 
    detailsDataAdapter.InsertCommand.Parameters.Add("@DID", System.Data.SqlDbType.Int, 4, "Discount Id"); 

    detailsDataAdapter.DeleteCommand = new SqlCommand(); 
    detailsDataAdapter.DeleteCommand.CommandText = "DELETE FROM [Project Customer Discount] " 
     + " WHERE ([Discount Id] = @DID);"; 
    detailsDataAdapter.DeleteCommand.Connection = conn; 

    detailsDataAdapter.DeleteCommand.Parameters.Add("@DID", System.Data.SqlDbType.Int, 4, "Discount Id"); 
} 

상위/마스터 데이터 테이블의 기본 키에 대한 autoIncrement를 생성합니다. Autoincrement는 -1에서 시작하여 -1만큼 증가하여 고유하게 유지됩니다.

dataGridView1.DataSource = masterBindingSource; 
dataGridView2.DataSource = detailsBindingSource; 
GetData(); 
: dataGridViews에

if (data.Tables["Discounts"].GetChanges(System.Data.DataRowState.Deleted) != null) 
{ 
    detailsBindingSource.EndEdit(); 
    System.Data.DataTable DeletedChildRecords = 
     data.Tables["Discounts"].GetChanges(System.Data.DataRowState.Deleted); 

    try 
    { 
     if (DeletedChildRecords != null) 
     { 
      SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(detailsDataAdapter); 
      detailsDataAdapter.Update(data.Tables["Discounts"].Select(null, null, DataViewRowState.Deleted)); 
     } 

    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString()); 
    } 
    finally 
    { 
     if (DeletedChildRecords != null) 
     { 
      DeletedChildRecords.Dispose(); 
     } 
    } 
} 

if (data.Tables["Customers"].GetChanges(System.Data.DataRowState.Added) != null || data.Tables["Customers"].GetChanges(System.Data.DataRowState.Modified) != null || 
    data.Tables["Customers"].GetChanges(System.Data.DataRowState.Deleted) != null) 
{ 
    masterBindingSource.EndEdit(); 
    try 
    { 
     SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(masterDataAdapter); 
     masterDataAdapter.Update(data.Tables["Customers"].Select(null, null, DataViewRowState.Deleted)); 
     masterDataAdapter.Update(data.Tables["Customers"].Select(null, null, DataViewRowState.ModifiedCurrent)); 
     masterDataAdapter.Update(data.Tables["Customers"].Select(null, null, DataViewRowState.Added)); 

    } 
    catch (System.Exception err) 
    { 
     MessageBox.Show(err.ToString());       
    } 
} 

if (data.Tables["Discounts"].GetChanges(System.Data.DataRowState.Added) != null || data.Tables["Discounts"].GetChanges(System.Data.DataRowState.Modified) != null) 
{ 
    detailsBindingSource.EndEdit(); 
    System.Data.DataTable NewChildRecords = 
     data.Tables["Discounts"].GetChanges(System.Data.DataRowState.Added); 

    System.Data.DataTable ModifiedChildRecords = 
     data.Tables["Discounts"].GetChanges(System.Data.DataRowState.Modified); 

    try 
    { 
     if (ModifiedChildRecords != null) 
     { 
      SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(detailsDataAdapter); 
      detailsDataAdapter.Update(data.Tables["Discounts"].Select(null, null, DataViewRowState.ModifiedCurrent)); 
     } 

     if (NewChildRecords != null) 
     { 
      SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(detailsDataAdapter); 
      detailsDataAdapter.Update(data.Tables["Discounts"].Select(null, null, DataViewRowState.Added)); 
     } 

    } 

    catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString()); 
    } 

    finally 
    { 
     if (NewChildRecords != null) 
     { 
      NewChildRecords.Dispose(); 
     } 
     if (ModifiedChildRecords != null) 
     { 
      ModifiedChildRecords.Dispose(); 
     } 
    } 
} 

바인딩 데이터 소스 : 데이터베이스에 다시 변경 사항을 저장

private void GetData() 
{ 
    try 
    { 

     conn = new SqlConnection(connstr); 
     conn.Open(); 

     // Create a DataSet. 
     data = new DataSet(); 
     data.Locale = System.Globalization.CultureInfo.InvariantCulture; 

     string sqlStr = "SELECT [Project Customer UBF].* FROM [Project Customer UBF]; "; //WHERE [Project Customer UBF].[Project Id] = " +_projectID;      

     // Add data from the Customers table to the DataSet. 
     masterDataAdapter = new 
      SqlDataAdapter(sqlStr, conn); 

     masterDataAdapter.Fill(data, "Customers"); 

     // Add data from the Orders table to the DataSet. 
     detailsDataAdapter = new 
      SqlDataAdapter("SELECT [Project Customer Discount].* FROM [Project Customer Discount]", conn); 
     detailsDataAdapter.Fill(data, "Discounts"); 

     detailsDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; 

     GenerateCommands(); 
     CreateRelations(); 

     // Bind the master data connector to the Customers table. 
     masterBindingSource.DataSource = data; 
     masterBindingSource.DataMember = "Customers"; 
     masterBindingSource.Filter = "[Project Id] =" + _projectID; 

     // Bind the details data connector to the master data connector, 
     // using the DataRelation name to filter the information in the 
     // details table based on the current row in the master table. 
     detailsBindingSource.DataSource = masterBindingSource; 
     detailsBindingSource.DataMember = "CustDist"; 
     conn.Close(); 

    } 
    catch (SqlException) 
    { 
     MessageBox.Show("To run this example, replace the value of the " + 
      "connectionString variable with a connection string that is " + 
      "valid for your system."); 
    } 
} 

: 마스터/부모 세부/하위 데이터 집합을 만들기위한

private void CreateRelations() { 
    DataRelation relation = new DataRelation("CustDist", 
     data.Tables["Customers"].Columns["Project Customer UBF Id"], 
     data.Tables["Discounts"].Columns["Project Customer UBF Id"]); 
    data.Relations.Add(relation); 

    data.Tables["Customers"].Columns["Project Customer UBF Id"].AutoIncrement = true; 
    data.Tables["Customers"].Columns["Project Customer UBF Id"].AutoIncrementSeed = -1; 
    data.Tables["Customers"].Columns["Project Customer UBF Id"].AutoIncrementStep = -1; 
} 

코드