내 중첩 된 gridview 검색을 구현했으며 모두 잘 작동합니다. 그러나 gridview로드 할 때 부모 테이블에 중복 행을 표시합니다. Select 문 잘못된 결과 표시
당신이 그림에서 볼 수 있듯이
는 AC107의 CourseID에서이 개 책이 있습니다. 그러나 내 Gridview 과정에서 각 교과서에 대한 행을 표시합니다. 나는이 select 문을 엉망으로 만들었고 어쨌든 어떤 것이 작동하는지, gridview가로드되지 않는지보기 위해 바꾼다. protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//i'm using a datatable for storing all the data
DataTable dt = new DataTable();
string query = "select * from Course inner join textBooks on textBooks.CourseID = Course.CourseID";
//wrapping in 'using' means the connection is closed an disposed when done
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HUTDMSConnectionString"].ToString()))
using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
{
try
{
//fill the datatable with the contents from the database
adapter.Fill(dt);
}
catch
{
}
}
//save the datatable into a viewstate for later use
ViewState["allBooks"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
다음은 내 데이터 테이블의 레이아웃입니다.
SELECT * FROM (
select Course.*,
ROW_NUMBER() OVER(PARTITION BY Course.CourseID ORDER BY Course.CourseID) AS rn
from Course
inner join textBooks
on textBooks.CourseID = Course.CourseID) xxx
WHERE rn = 1;
(당신이
SqlConnection
바이더 클래스를 사용하고 있기 때문에 당신이
SQL Server
를 사용하는 가정)
빈 try-catch를 절대로 사용하지 마십시오. – LarsTech