asp.net 开发用vb语言编写“查找”功能代码
DropDownList控件+DataGrid控件+SQL Server 实现查找并显示信息。根据DropDownList选择的内容搜索数据库中的字段用DataGrid输出信息,
需要完整的代码。
DropDownList控件+DataGrid控件+SQL Server 实现查找并显示信息。根据DropDownList选择的内容搜索数据库中的字段用DataGrid输出信息,
需要完整的代码。
答案:SqlConnection conn = new SqlConnection("Server=WWW-2270B8786BB;Integrated Security=SSPI;Database=Northwind;Uid=sa;Pwd=sa;");protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{string sqlStr = "select * from Orders ";
SqlCommand cmd = new SqlCommand(sqlStr, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);DataSet ds = new DataSet();
da.Fill(ds);
this.DropDownList1.DataSource = ds.Tables[0].DefaultView;
DropDownList1.DataTextField = "CustomerID";
DropDownList1.DataValueField = "OrderID";
DropDownList1.DataBind();
}}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string val = DropDownList1.SelectedValue;string sqlStr = "select * from Orders where OrderID= '" + val + "'";
SqlCommand cmd = new SqlCommand(sqlStr, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);DataSet ds = new DataSet();
da.Fill(ds);this.DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}