我的更新命令无法从DataGridView C#更新数据库

时间:2015-10-26 06:56:14

标签: c# datagridview

这是我从datagridview更新数据库的代码,如果有任何正文有答案请告诉我。

try
{
     if (string.IsNullOrEmpty(tbName.Text) || string.IsNullOrEmpty(cmbSex.Text) || string.IsNullOrEmpty(cmbFeesAmount.Text) || string.IsNullOrEmpty(cmbFeesStatus.Text) || string.IsNullOrEmpty(cmbSex.Text) || string.IsNullOrEmpty(Date.Text)) { MessageBox.Show("Please Input data."); return; }
         Update(lblDI.Text);
         Reset();
         this.BindGrid();
}
catch (Exception ex)
{
     MessageBox.Show(ex.ToString());
}

1 个答案:

答案 0 :(得分:0)

我对代码进行了一些编辑,现在它对我来说很好。我忘了申报sda.DeleteCommand = cb.GetDeleteCommand();

private static void CurdOperation()
    {
        SqlCeConnection con = new SqlCeConnection("Data Source="
            + System.IO.Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "MyDB.sdf"));
        sda = new SqlCeDataAdapter();
        SqlCeCommand cmd = con.CreateCommand();
        cmd.CommandText = "select * from tblFees";
        sda.SelectCommand = cmd;

        SqlCeCommandBuilder cb = new SqlCeCommandBuilder(sda);
        sda.InsertCommand = cb.GetInsertCommand();
        sda.UpdateCommand = cb.GetUpdateCommand();
        sda.DeleteCommand = cb.GetDeleteCommand();
    }
    #endregion
try
{
     if (string.IsNullOrEmpty(tbName.Text) || string.IsNullOrEmpty(cmbSex.Text) || string.IsNullOrEmpty(cmbFeesAmount.Text) || string.IsNullOrEmpty(cmbFeesStatus.Text) || string.IsNullOrEmpty(cmbSex.Text) || string.IsNullOrEmpty(Date.Text)) { MessageBox.Show("Please Input data."); return; }
         Update(lblDI.Text);
         Reset();
         this.BindGrid();
}
catch (Exception ex)
{
     MessageBox.Show(ex.ToString());
}

现在可行。