1
누구든지이 코드를 도와 줄 수 있습니까? 레코드를 업데이트하려고하는데이 줄에 The DataAdapter.SelectCommand property needs to be initialized.
이 오류가 발생합니다. data_adapter.UpdateCommand = builder.GetUpdateCommand();
수정 방법을 알 수 없습니다.ADOBE 테이블의 레코드를 업데이트 할 때의 문제
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); }
}
}
하기 권장 코드 ", ConnectionState);" Plane은 내가 업데이트하고 싶은 테이블입니다. 그러나 다음과 같은 오류가 발생합니다. 오류 인수 2 : 'System.Data.ConnectionState'에서 'string'으로 변환 할 수 없거나 오류 인수 2 : 'System.Data.ConnectionState'에서 'System'으로 변환 할 수 없습니다. Data.SqlClient.SqlConnection ' – user3421241
ConnectionState를 연결로 변경하십시오. – Shell
'data_adapter' 객체가 생성 된 클래스 수준에서'childDataAdapter'를 선언하고 data_adapter 대신이 클래스를 사용하십시오. – Shell