在数据库上获取和显示表

时间:2012-03-13 10:10:10

标签: c# asp.net

我刚刚开始使用asp.net c#,我已经完成了一项任务,可以在下拉列表中根据给定的值生成可用的医生。

我有3个下拉列表,(1)PROVINCE,(2)CITY,(3)SPECIALIZATION和搜索按钮。 在用户选择3个下拉列表的值并点击搜索按钮后,它将打印一个包含可用医生的表。

我知道密钥在搜索按钮上,但我不知道在搜索按钮下放什么。你能帮帮我吗?

这是我的代码:

       protected void Page_Load(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(2000);

        if (!IsPostBack)
        {
            string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
            SqlConnection conn = new SqlConnection(constring);
            DataTable dt = new DataTable("emed_province");

            using (conn)
            {
                conn.Open();
                SqlCommand comm = new SqlCommand("SELECT *  FROM emed_province ORDER BY PROVINCE_NAME ASC", conn);
                SqlDataAdapter adptr = new SqlDataAdapter(comm);
                adptr.Fill(dt);
            }

            ddlProvince.DataSource = dt;
            ddlProvince.DataTextField = "PROVINCE_NAME";
            ddlProvince.DataValueField = "PROVINCE_CODE";
            ddlProvince.DataBind();
        }
    }

    protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(2000);

        string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
        SqlConnection conn = new SqlConnection(constring);
        DataTable dt = new DataTable("emed_province");

        using (conn)
        {
            conn.Open();
            SqlCommand comm = new SqlCommand("SELECT * FROM emed_city WHERE PROVINCE_CODE =@pcode", conn);
            comm.Parameters.AddWithValue("@pcode", ddlProvince.SelectedValue);
            SqlDataAdapter adptr = new SqlDataAdapter(comm);
            adptr.Fill(dt);

            SqlParameter param = new SqlParameter();
            param.ParameterName = "@pcode";
            param.Value = ddlProvince;

            comm.Parameters.Add(param);
        }
        ddlCity.DataSource = dt;
        ddlCity.DataTextField = "CITY_NAME";
        ddlCity.DataValueField = "CITY_CODE";
        ddlCity.DataBind();
    }

    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(2000);

        string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
        SqlConnection conn = new SqlConnection(constring);
        DataTable dt = new DataTable("emed_city");

        using (conn)
        {
            conn.Open();
            SqlCommand comm = new SqlCommand("select distinct emed_accredited_providers.SPECIALIZATION from emed_accredited_providers inner join emed_doctors_hospitals on emed_accredited_providers.DOCTOR_CODE = emed_doctors_hospitals.DOCTOR_CODE where CITY_CODE =@ccode", conn);
            comm.Parameters.AddWithValue("@ccode", ddlCity.SelectedValue);
            SqlDataAdapter adptr = new SqlDataAdapter(comm);
            adptr.Fill(dt);

            SqlParameter param = new SqlParameter();
            param.ParameterName = "@ccode";
            param.Value = ddlCity;

            comm.Parameters.Add(param);
        }
        ddlSpec.DataSource = dt;
        ddlSpec.DataTextField = "SPECIALIZATION";
        ddlSpec.DataValueField = "SPECIALIZATION";
        ddlSpec.DataBind();
    }

    protected void btnDocs_Click(object sender, EventArgs e)
    {

    }
}
}

2 个答案:

答案 0 :(得分:0)

  1. 从下拉列表中获取所选值
  2. 将所选值传递给搜索功能。 (这取决于你的逻辑,我的意思是如果用户只从一个下拉列表中选择值,只有两个下拉列表可能会有所不同)。
  3. 从搜索功能中获取返回值并将其绑定到您的表格(网格视图)
  4. 另请查看DropDownList.SelectedIndex Property

    绑定gridview你可以做类似的事情

    private void BindGrid ()
    { 
    
            var dt = new DataTable();
            var connection = new SqlConnection(YOUR CONNECTION); 
    
            try
            {
              connection.Open();
              var query = "YOUR SEARCH QUERY";
              var sqlCmd = new SqlCommand(query, connection);
              var sqlDa = new SqlDataAdapter(sqlCmd);
              sqlDa.Fill(dt);
    
                  if (dt.Rows.Count > 0)
                  {
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                  }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                    //
            }
            finally
            {
                connection.Close();
            }
    }
    

答案 1 :(得分:0)

string query = string.Empty;

 if (ddlProvince.SelectedIndex != -1)
                {
                    query = query + " and PROVINCE_CODE=" + ddlProvince.SelectedValue;
                }

                if (ddlCity.SelectedIndex != -1)
                {
                    query = query + " and CITY_CODE=" + ddlProvince.SelectedValue;
                }

                if (ddlSpec.SelectedIndex != -1)
                {
                    query = query + " and SPECIALIZATION=" + ddlProvince.SelectedValue;
                }


                string tQuery = "Select * from Doc";
                if (query.Length > 0)
                {
                    query = query.Remove(0, 4);
                    tQuery = tQuery + query;
                }
                // Exeucate -- tQuery

//修改表名或字段名。