簡體   English   中英

如何從SQL Server數據庫表列中獲取Male或Female並在轉發器控件中顯示記錄?

[英]How to fetch Male or Female from a SQL Server database table column and display the records in a repeater control?

ASP.NET,C#和SQL Server數據庫。

我有一個帶有gender列的表格Students ,並將照片保存在另一個表格中。 在頁面上,我有一個下拉控件,已將其綁定到性別值,還有一個按鈕。

我想做的是:如果用戶從下拉控件中選擇男性或女性,然后單擊按鈕,則所有選擇的照片和記錄都應顯示在轉發器控件中。

我嘗試將存儲過程與查詢結合使用:

select A.*, B.*  
from tblStudents A 
cross apply
    (select top 1 * 
     from tbl studentImages B 
     where A.Gender = @Gender and B.ImageID = A.ImageID

但是它顯示所有記錄,而不是僅顯示所選記錄。

有人可以幫忙嗎?

后面的代碼:

protected void btnsearch_click(onject sender, EventArgs)
{  
      Int64 Gender = Convert.Toint64(Request.Querystring[Gender]);

       String S = ConfigurationManager.onnectionStrings["DBST"]ConnectionStrings;

       using(SqlCommand con= new SqlCommand(CS))
       {
             SqlCommand cmd = new SqlCommand("SP_Data",con)
             cmd.commandType = commandType.StoredProcedure;

             con.Open();

             SqlDataAdapter ada = new SqlDataAdapter(cmd)

             DataTable dt = new DataTable();
             ada.Fill(dt);

             if(dt.Rows.Count !=0)
             {
                 rpterGender.DataSource = dt;
                 rpterGender.DataBind();
             }
        }
    }
}

您需要在查詢中使用連接。

select A.*, B.*  
from tblStudents A 
join tblstudentImages B on B.ImageID = A.ImageID
where A.Gender = @Gender

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM