c#添加,更新,删除不在DGV和db中工作

时间:2017-01-22 00:25:50

标签: c#

我的db和DGV在添加新记录时没有更新!

form design

哪里错了? 我正在尝试解决这个问题但仍然存在 (添加,删除,更新) 我认为所有这些都有同样的问题 也许数据集,需要一些帮助

//我的数据集名称:emp2 //我的数据库表名:emp_table

    private void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            Add();
            Reset();
            this.BindGrid();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    private void brnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(txtId.Text) || string.IsNullOrEmpty(txtName.Text) || string.IsNullOrEmpty(txtDep.Text) || string.IsNullOrEmpty(txtAge.Text) || string.IsNullOrEmpty(txtQualification.Text) || string.IsNullOrEmpty(txtMob.Text) || string.IsNullOrEmpty(txtEmail.Text)) { MessageBox.Show("Please Input data."); return; }
            Update(lblId.Text);
            Reset();
            this.BindGrid();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(txtId.Text) || string.IsNullOrEmpty(txtName.Text) || string.IsNullOrEmpty(txtDep.Text) || string.IsNullOrEmpty(txtAge.Text) || string.IsNullOrEmpty(txtQualification.Text) || string.IsNullOrEmpty(txtMob.Text) || string.IsNullOrEmpty(txtEmail.Text)) { MessageBox.Show("Please Input data."); return; }
            Delete(lblId.Text);
            Reset();
            this.BindGrid();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }


    }

    private void dgvOldData_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (emp_tableDataGridView.Rows.Count > 0 && e.RowIndex != -1)
        {
            if (emp_tableDataGridView.Rows[e.RowIndex].Cells[0].Selected)
            {
                txtId.Text = emp_tableDataGridView.Rows[e.RowIndex].Cells[1].Value.ToString();
                txtName.Text = emp_tableDataGridView.Rows[e.RowIndex].Cells[2].Value.ToString();
                txtDep.Text = emp_tableDataGridView.Rows[e.RowIndex].Cells[3].Value.ToString();
                txtAge.Text = emp_tableDataGridView.Rows[e.RowIndex].Cells[4].Value.ToString();
                txtQualification.Text = emp_tableDataGridView.Rows[e.RowIndex].Cells[5].Value.ToString();
                txtMob.Text = emp_tableDataGridView.Rows[e.RowIndex].Cells[6].Value.ToString();
                txtEmail.Text = emp_tableDataGridView.Rows[e.RowIndex].Cells[7].Value.ToString();
                lblId.Text = emp_tableDataGridView.Rows[e.RowIndex].Cells[4].Value.ToString();

                btnDelete.Enabled = true;
                brnUpdate.Enabled = true;
                btnAdd.Enabled = false;

            }
        }
    }

    private static void FillDataInDataset(out DataSet oldData, out DataRow dr)
    {
        oldData = new DataSet();
        dr = null;
        sda.Fill(oldData);
    }

    private void BindGrid()
    {
        DataSet _ds = new DataSet();
        sda.Fill(_ds);
        if (_ds.Tables.Count > 0)
        {
            emp_tableDataGridView.DataSource = _ds.Tables[0];
        }

    }

    private void Add()
    {
        DataSet oldData;
        DataRow dr;
        FillDataInDataset(out oldData, out dr);
        //create new row assign value to it.
        dr = oldData.Tables[0].NewRow();
        if (string.IsNullOrEmpty(txtId.Text) || string.IsNullOrEmpty(txtName.Text) || string.IsNullOrEmpty(txtDep.Text) || string.IsNullOrEmpty(txtAge.Text) || string.IsNullOrEmpty(txtQualification.Text) || string.IsNullOrEmpty(txtMob.Text) || string.IsNullOrEmpty(txtEmail.Text)) { MessageBox.Show("Please Input data."); return; }
        dr["emp_id"] = txtId.Text.Trim();
        dr["name"] = txtName.Text.Trim();
        dr["dep"] = txtDep.Text.Trim();
        dr["emp_deg"] = txtAge.Text.Trim();
        dr["emp_pos"] = txtQualification.Text.Trim();
        dr["phone"] = txtMob.Text.Trim();
        dr["email"] = txtEmail.Text.Trim();
        oldData.Tables[0].Rows.Add(dr);
        sda.Update(oldData);

    }

    private void Update(string id)
    {
        DataSet oldData;
        DataRow dr;
        FillDataInDataset(out oldData, out dr);
        //Here get record of specified id.
        DataRow[] tempdata = oldData.Tables[0].AsEnumerable().Where(p => p["index"].ToString() == id).ToArray();
        if (tempdata.Length > 0)
        {
            dr = tempdata[0];
            dr["emp_id"] = txtId.Text.Trim();
            dr["name"] = txtName.Text.Trim();
            dr["dep"] = txtDep.Text.Trim();
            dr["emp_deg"] = txtAge.Text.Trim();
            dr["emp_pos"] = txtQualification.Text.Trim();
            dr["phone"] = txtMob.Text.Trim();
            dr["email"] = txtEmail.Text.Trim();

        }
        sda.Update(oldData);
    }

    private void Delete(string id)
    {
        DataSet oldData;
        DataRow dr;
        FillDataInDataset(out oldData, out dr);
        //Here get record of specified id.
        DataRow[] tempdata = oldData.Tables[0].AsEnumerable().Where(p => p["index"].ToString() == id).ToArray();
        if (tempdata.Length > 0)
        {
            dr = tempdata[0];
            dr["emp_id"] = txtId.Text.Trim();
            dr["name"] = txtName.Text.Trim();
            dr["dep"] = txtDep.Text.Trim();
            dr["emp_deg"] = txtAge.Text.Trim();
            dr["emp_pos"] = txtQualification.Text.Trim();
            dr["phone"] = txtMob.Text.Trim();
            dr["email"] = txtEmail.Text.Trim();
            dr.Delete();
        }
        sda.Update(oldData);

    }

    private void Reset()
    {
        txtId.Text = string.Empty;
        txtName.Text = string.Empty;
        txtDep.Text = string.Empty;
        txtAge.Text = string.Empty;
        txtQualification.Text = string.Empty;
        txtMob.Text = string.Empty;
        txtEmail.Text = string.Empty;
        btnDelete.Enabled = false;
        brnUpdate.Enabled = false;
        btnAdd.Enabled = true;
    }

1 个答案:

答案 0 :(得分:0)

启用Sql Profiler并在运行应用程序时获取查询..这将有助于解决问题