如何基于两个表执行SQL查询! [英] How to perform an SQL query based on two tables!
问题描述
基本上,我必须执行搜索,我想在数据网格上显示会员的所有即将发生的事件。
困难的部分是, SpecialMoment表是所有事件的所在,包括MomentDateTime(这是我要去的地方> = GETDATE()来显示当前日期即将发生的所有事件)。但另一个困难的部分是MemberMomentNo号码在MemberMoment表中。
基本上我想在网格中显示:
MomentName,MomentDateTime - 所有在SpecialMoment表中
MemberID,MemberMomentNo - 所有在MemberMoment表中。
我基本上只搜索了2张桌子。
< br $>
我需要帮助谢谢
Basically, i have to perform a search where i want to show all the upcoming events for a member on a datagrid.
The hard part is that, the SpecialMoment table is where all the events are, including the MomentDateTime (which is where i was going to do >= GETDATE() to show all events upcoming from the current date). But another hard part is that the MemberMomentNo number is in the MemberMoment table.
Basically i want to show this in the grid:
MomentName, MomentDateTime - All in the SpecialMoment table
MemberID, MemberMomentNo - All in the MemberMoment table.
Im basically combing a searching through 2 tables.
I need help thanks
推荐答案
让我们拼出这个场景:
2桌:
表:会员,至少2个字段:memberid,membermomentno,...
表:时刻,至少2 fields:momentno,momentname,...
membermomentno字段链接到momentno
假设您要从这2个表中绘制数据并填充数据网格。调整以下代码以满足您的需求:
aspx页面上的
:
Let spell out the scenario:
2 tables:
table: membermember, at least 2 fields: memberid, membermomentno, ...
table: moment, at least 2 fields: momentno, momentname, ...
membermomentno field is linked to momentno
Say you want to draw data from this 2 tables and populate a datagrid. Adapt the following code to suit your needs:
on the aspx page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default8.aspx.cs" Inherits="Default8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="800"
BackColor="#cccccc"
BorderColor="black"
ShowFooter="false"
CellPadding="5"
CellSpacing="5"
Font-Name="Verdana"
Font-Size="12pt"
HeaderStyle-BackColor="#abcdef"
EnableViewState="false"
/>
</div>
</form>
</body>
</html>
代码背后:
on the code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Default8 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Users\Peter Leow\Documents\Visual Studio 2010\Projects\SampleDatabaseWalkthrough\SampleDatabaseWalkthrough\SampleDatabase.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT memberid, momentname FROM membermoment JOIN moment ON membermomentno = momentno", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds);
DataView source = new DataView(ds.Tables[0]);
MyDataGrid.DataSource = source;
MyDataGrid.DataBind();
}
}
这篇关于如何基于两个表执行SQL查询!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!