在ado.net中更新表中的记录时出现问题

本文关键字:问题 记录 ado net 更新 | 更新日期: 2023-09-27 17:57:56

有人能帮我处理这段代码吗。我正在尝试更新一条记录,但我在这行data_adapter.UpdateCommand = builder.GetUpdateCommand();中收到了这个错误The DataAdapter.SelectCommand property needs to be initialized.。我不知道如何修复它。

public partial class AirlineReservation : Form
    {
        public SqlConnection connection = new SqlConnection("Data Source=(local); Initial Catalog=Airline_Reservation; Integrated Security=SSPI");
        public SqlDataAdapter data_adapter = new SqlDataAdapter();
        public DataSet ds = new DataSet();
        public DataGridView parentDataGridView = new DataGridView();
        public BindingSource parentBindingSource = new BindingSource();
        public DataGridView childDataGridView = new DataGridView();
        public BindingSource childBindingSource = new BindingSource();
        SqlCommand select_planes = new SqlCommand();
        SqlCommand select_airlines = new SqlCommand();
        public SqlCommandBuilder builder;
        public DataRelation rel;
        public AirlineReservation()
        {
            InitializeComponent();           
        }
        private void getData()
        {
            SqlDataAdapter parentDataAdapter = new SqlDataAdapter("select * from Airline", connection);
            parentDataAdapter.Fill(ds, "Airline");
            SqlDataAdapter childDataAdapter = new SqlDataAdapter("select * from Plane", connection);
            childDataAdapter.Fill(ds, "Plane");
            DataColumn parentColumn = ds.Tables["Airline"].Columns["airline_id"];
            DataColumn childColumn = ds.Tables["Plane"].Columns["airline_id"];
            rel = new DataRelation("PlaneAirline", parentColumn, childColumn);
            ds.Relations.Add(rel);
            parentBindingSource.DataSource = ds;
            parentBindingSource.DataMember = "Airline";
            childBindingSource.DataSource = parentBindingSource;
            childBindingSource.DataMember = "PlaneAirline";   
        }
        private void AirlineReservation_Load(object sender, EventArgs e)
        {
            parentDataGridView.DataSource = parentBindingSource;
            childDataGridView.DataSource = childBindingSource;
            getData();  
        }
        private void dg_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataView dv = new DataView(ds.Tables["Plane"], "airline_id = " + dg.CurrentRow.Cells[0].Value, "", DataViewRowState.CurrentRows);
            dg2.DataSource = dv;
        }
        private void display_btn_Click(object sender, EventArgs e)
        {
            dg.DataSource = ds.Tables[0];
        }
        private void dg2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = dg2.Rows[e.RowIndex];
            aidbox.Text = row.Cells["airline_id"].Value.ToString();
            pid_box.Text = row.Cells["plane_id"].Value.ToString();
            name_box.Text = row.Cells["name"].Value.ToString();
            model_box.Text = row.Cells["model"].Value.ToString();
            fc_box.Text = row.Cells["f_seats"].Value.ToString();
            sc_box.Text = row.Cells["s_seats"].Value.ToString();
            bs_box.Text = row.Cells["b_seats"].Value.ToString();
            weight_box.Text = row.Cells["p_weight"].Value.ToString();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            DataRow[] row_update = ds.Tables["Plane"].Select("airline_id = " + aidbox.Text);
            try
            {
                var row = row_update[0];
                row["airline_id"] = int.Parse(aidbox.Text);
                row["plane_id"] = int.Parse(pid_box.Text);
                row["name"] = name_box.Text;
                row["model"] = model_box.Text;
                row["f_seats"] = int.Parse(fc_box.Text);
                row["s_seats"] = int.Parse(sc_box.Text);
                row["b_seats"] = int.Parse(bs_box.Text);
                row["p_weight"] = float.Parse(weight_box.Text);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            try
                {
                    builder = new SqlCommandBuilder(data_adapter);
                    data_adapter.UpdateCommand = builder.GetUpdateCommand();
                    data_adapter.Update(ds, "Plane");
                }
                catch (SqlException ex) { MessageBox.Show(ex.Message); }
            }
        }

在ado.net中更新表中的记录时出现问题

您没有正确初始化data_adapter对象。

在这一行中,您刚刚创建了一个数据适配器的对象

public SqlDataAdapter data_adapter = new SqlDataAdapter();

您需要在其中指定select命令。

public SqlDataAdapter data_adapter = new SqlDataAdapter();
data_adapter.SelectCommand = new SqlCommand("Select Query", connection);

或在声明中指定。

public SqlDataAdapter data_adapter = new SqlDataAdapter("Select Query", connection);

建议的代码

//Create this object at class level
public partial class AirlineReservation
{
    SqlDataAdapter childDataAdapter;
...
...
 private void getData()
 {
     use it like this in getData() function
     childDataAdapter = new SqlDataAdapter("select * from Plane", connection);
     ...
     ...

private void button2_Click(object sender, EventArgs e)
{
    DataRow[] row_update = ds.Tables["Plane"].Select("airline_id = " + aidbox.Text);
    try
    {
        var row = row_update[0];
        row["airline_id"] = int.Parse(aidbox.Text);
        row["plane_id"] = int.Parse(pid_box.Text);
        row["name"] = name_box.Text;
        row["model"] = model_box.Text;
        row["f_seats"] = int.Parse(fc_box.Text);
        row["s_seats"] = int.Parse(sc_box.Text);
        row["b_seats"] = int.Parse(bs_box.Text);
        row["p_weight"] = float.Parse(weight_box.Text);
    }
    catch (Exception ex) { MessageBox.Show(ex.Message); }
    try
        {
            builder = new SqlCommandBuilder(childDataAdapter);
                    //This line is not required
            //data_adapter.UpdateCommand = builder.GetUpdateCommand();
                    ds.EndInit();
            childDataAdapter.Update(ds, "Plane");
        }
        catch (SqlException ex) { MessageBox.Show(ex.Message); }
    }
}