在ASP.NET中,怎么把DropDownList的内容添加到Access数据库中呢。急用!

2024-11-25 11:01:55
推荐回答(2个)
回答1:

首先你得获得DropDownList的SelectedValue的值,而不是SelectedText,然后组织SQL语句插入即可了。

回答2:

try
{
myConn.Open();

DataSet ds = new DataSet();
da = new SqlDataAdapter("Select * from Passenger", myConn);

da.Fill(ds, "Passenger");

da.InsertCommand = myConn.CreateCommand();
da.InsertCommand.CommandText = "insert INTO Passenger values('" + this.txtFlightCode.Text + "','" + this.txtPassPort.Text + "','" +
this.txtName.Text + "','" + this.txtSeatNo.Text + "','" + this.cboSex.SelectedItem.ToString() + "','" + Convert.ToInt16(this.txtAge.Text )+ "')";

DataTable dt = new DataTable();
ds.Tables.Add(dt);
DataRow dr = ds.Tables[0].NewRow();

dr["FlightCode"] = this.txtFlightCode.Text;
dr["PassPortNo"] = this.txtPassPort.Text;
dr["Name"] = this.txtName.Text;
dr["SeatNo"] = this.txtSeatNo.Text;
dr["Sex"] = this.cboSex.SelectedValue; ///////你要的是这段代码
dr["Age"] = this.txtAge.Text;
ds.Tables[0].Rows.Add(dr);

da.Update(ds, "Passenger");
Application.DoEvents();
MessageBox.Show("信息成功添加!");

ClearTextMothed();
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
finally
{
myConn.Close();
}

往数据库中添加的方法很多,这里给你一种