请教asp.net(c#)

来源:百度知道 编辑:UC知道 时间:2024/06/30 15:43:59
程序如下: string strconn = "provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath("mydb.mdb");
OleDbConnection conn = new OleDbConnection(strconn);
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{

string sqlstr = "select distinct 作者 from 书籍1 ";
conn.Open();
OleDbCommand olecmd = new OleDbCommand(sqlstr,conn);

DropDownList1.DataSource = olecmd.ExecuteReader();
DropDownList1.DataTextField = "作者";
// DropDownList1.DataValueField = "编号";
DropDownList1.DataBind();
conn.Close();

}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

string sqlstr = "select * from 书籍1 where 作者='"+ DropDownList1.SelectedItem.Value + "'";

改为这样应该可以了:
string strconn ;
OleDbConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
strconn = "provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath("mydb.mdb");
conn = new OleDbConnection(strconn);

if (!IsPostBack)
{

string sqlstr = "select distinct 作者 from 书籍1 ";
conn.Open();
OleDbCommand olecmd = new OleDbCommand(sqlstr,conn);

DropDownList1.DataSource = olecmd.ExecuteReader();
DropDownList1.DataTextField = "作者";
// DropDownList1.DataValueField = "编号";
DropDownList1.DataBind();
conn.Close();

}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

string sqlstr = "select * from 书籍1 where 作者='"+ DropDownList1.SelectedItem.Value + "'";
conn.Open();
OleDbCommand oledbcmd = new OleDbComma