【问题标题】:How to fetch Male or Female from a SQL Server database table column and display the records in a repeater control?如何从 SQL Server 数据库表列中获取男性或女性并在转发器控件中显示记录?
【发布时间】:2019-06-24 17:32:37
【问题描述】:

ASP.NET、C# 和 SQL Server 数据库。

我有一个表Students,其中有一个gender 列,照片保存在另一个表中。 在页面上,我有一个已绑定到性别值的下拉控件和一个按钮。

我要做的是:如果用户从下拉控件中选择男性或女性并单击按钮,则所有选定的照片和记录都应显示在转发器控件中..

我已经尝试使用存储过程进行查询:

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();
             }
        }
    }
}

【问题讨论】:

  • 可以编辑代码标签包围的sql查询吗?很难读。
  • 你为什么不尝试加入? select {A.student info... B.StudentImage} from tblStudents A join studentImages on A.StudentId = b.StudentId where A.Gender= @Gender
  • 你的 where 语句是在 apply() 函数内部还是外部?它缺少一个括号。它应该在 apply() 之外,因为您希望表 A 而不是 B 上的 where 子句。这将过滤掉记录。

标签: c# asp.net sql-server


【解决方案1】:

你需要在Query中使用join。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    相关资源
    最近更新 更多