更改表后通过datagridview更新数据库

时间:2014-08-26 04:58:24

标签: c# datagridview

我遇到了datatableadapter.update(dataset)的问题。一切正常,直到我向数据库添加一个新列,它将显示新项目但是当我尝试通过dataviewgrid更新值时。我没有错误,但数据没有保存。这是代码。

    private void button2_Click(object sender, EventArgs e)
    {

        MessageBox.Show("Record Updated");
        this.inventoryTableAdapter.Update(jobDetailsDataSet.Inventory);



    }

    private void button1_Click(object sender, EventArgs e)
    {
       this.inventoryTableAdapter.Fill(this.jobDetailsDataSet.Inventory);  


    }



    private void btnadd_Click(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection(global::FarWest_Program.Properties.Settings.Default.JobDetailsConnectionString);


        try
        {
            string sql;

            sql = "ALTER TABLE Inventory ADD " + txtitem.Text + " VARCHAR (50)";
            this.inventoryTableAdapter.Fill(this.jobDetailsDataSet.Inventory);           
            SqlCommand execommand = new SqlCommand(sql, cn);
            cn.Open();
            execommand.ExecuteNonQuery();
            MessageBox.Show("You have Updated The Job Details", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + Environment.NewLine + "Please Check to See you only used Numbers", "Only Numbers Allowed", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }
        finally
        {
            cn.Close();
        }
    }

0 个答案:

没有答案
相关问题