Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向主/明细数据集添加多行时出现约束错误_C#_Datagridview_Master Detail_Datarelation - Fatal编程技术网

C# 向主/明细数据集添加多行时出现约束错误

C# 向主/明细数据集添加多行时出现约束错误,c#,datagridview,master-detail,datarelation,C#,Datagridview,Master Detail,Datarelation,我有两个绑定到主/详细关系的DataGridView。 当我尝试向主dataGridView添加第二行时,我得到以下错误 system.data.constraintexception列“项目客户UBF Id”被约束为唯一。值“”已存在于数据关系中 我可以向子DataGridView添加多行,如果删除表之间的DataRelation,我可以向主视图添加多行。 此外,如果我用下一个自动增量值手动输入主控键,我可以添加多行,以及在这些行中添加详细信息 这些表是使用SQL server中的自动增量主键

我有两个绑定到主/详细关系的DataGridView。 当我尝试向主dataGridView添加第二行时,我得到以下错误

system.data.constraintexception列“项目客户UBF Id”被约束为唯一。值“”已存在于数据关系中

我可以向子DataGridView添加多行,如果删除表之间的DataRelation,我可以向主视图添加多行。 此外,如果我用下一个自动增量值手动输入主控键,我可以添加多行,以及在这些行中添加详细信息

这些表是使用SQL server中的自动增量主键设置的

如能帮助克服此错误,将不胜感激

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();
}

通过为主/父数据表和详细信息/子数据表生成自己的更新、删除和添加命令来解决:

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");
}
以及为父/主数据表主键创建自动增量。自动递增从-1开始,递增-1以保持唯一性

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;
}
用于创建主/父详细信息/子数据集的代码:

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.");
    }
}
将更改保存回数据库:

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();
        }
    }
}
将数据源绑定到DataGridView:

dataGridView1.DataSource = masterBindingSource;
dataGridView2.DataSource = detailsBindingSource;
GetData();