繁体   English   中英

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

[英]Problems updating a record in a table in ado.net

谁能帮我这个代码。 我正在尝试更新一条记录,但出现此错误The DataAdapter.SelectCommand property needs to be initialized. 在这一行中data_adapter.UpdateCommand = builder.GetUpdateCommand(); 我不知道如何解决它。

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); }
            }

        }

您尚未正确初始化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); } } } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM