无法使用DataGridView将数据保存在我的数据库中

时间:2015-07-23 11:25:06

标签: c#

我创建了一个可以使用DataGridView从数据库添加,查看,更新和删除数据的应用程序。我已经使用DGV为每个操作制作了单独的表格。在更新表格中,我无法更新或编辑数据DGV,我的数据也没有存储在DGV的数据库中。请告诉我正确的代码。它是我的代码

    public Update()
    {
        InitializeComponent();
    }

    SqlConnection sql_con;
    SqlCommand sql_cmd;
    SqlDataAdapter Sql_ad;
    SqlCommandBuilder sql_cb;
    DataTable dt;
    DataSet ds;
    string connect_string;

    private void Update_Load(object sender, EventArgs e)
    {
        dataGridView1.Refresh();

        connect_string = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Administrator\Desktop\form\form\EmployeeDB.mdf;Integrated Security=True;User Instance=True";   

        // TODO: This line of code loads data into the 'ds_EmployeeDB.emp_info' table. You can move, or remove it, as needed.
        this.emp_infoTableAdapter.Fill(this.ds_EmployeeDB.emp_info);


        string select_cmd = "SELECT * FROM emp_info";
        sql_con = new SqlConnection(connect_string);
        sql_con.Open();
        sql_cmd = new SqlCommand(select_cmd,sql_con);
        Sql_ad = new SqlDataAdapter(sql_cmd);
        sql_cb = new SqlCommandBuilder(Sql_ad);
        ds = new DataSet();
        Sql_ad.Fill(ds,"emp_info");
        dt=ds.Tables["emp_info"];
        sql_con.Close();
        dataGridView1.DataSource = dt;

    }

    private void button_Search_Click(object sender, EventArgs e)
    {
        string select_cmd = "SELECT * FROM emp_info where f_name='" + textBox_Search.Text + "'";

        sql_con = new SqlConnection(connect_string);
        dt = new DataTable();
        Sql_ad = new SqlDataAdapter(select_cmd, sql_con);
        Sql_ad.Fill(dt);
        dataGridView1.DataSource = dt;
    }  

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        dataGridView1.Refresh();
    }

    private void button_Update_Click(object sender, EventArgs e)
    {

        string update_query = "UPDATE emp_info SET " +
            "f_name = '" + dataGridView1.SelectedRows[0].Cells[1].Value.ToString() + "' ," +
            "l_name = '" + dataGridView1.SelectedRows[0].Cells[2].Value.ToString() + "' ," +
            "father_name = '" + dataGridView1.SelectedRows[0].Cells[3].Value.ToString() + "' ," +
            "education = '" + dataGridView1.SelectedRows[0].Cells[4].Value.ToString() + "' ," +
            "institute = '" + dataGridView1.SelectedRows[0].Cells[5].Value.ToString() + "' ," +
            "address = '" + dataGridView1.SelectedRows[0].Cells[6].Value.ToString() + "' ," +
            "phone = '" + dataGridView1.SelectedRows[0].Cells[7].Value.ToString() + "' ," +
            "cinc = '" + dataGridView1.SelectedRows[0].Cells[8].Value.ToString() + "' ," +
            "WHERE '" + textBox_Search.Text + "'";

        sql_con = new SqlConnection(connect_string);
        sql_con.Open();
        sql_cmd = new SqlCommand(update_query, sql_con);
        Sql_ad = new SqlDataAdapter(sql_cmd);
        sql_cb = new SqlCommandBuilder(Sql_ad);
        ds = new DataSet();
        Sql_ad.Fill(ds, "emp_info");
        dt = ds.Tables["emp_info"];
        sql_con.Close();
        dataGridView1.DataSource = dt;
    }

    private void button_Cancle_Click(object sender, EventArgs e)
    {
        Close();
    }
}

}

0 个答案:

没有答案