C#主从表查询
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DA
{
/// <summary>
/// StudentInf 的摘要说明。
/// </summary>
public class StudentInf :IDisposable
{
private SqlConnection con;
private SqlDataAdapter da;
public StudentInf()
{
//
// TODO: 在此处添加构造函数逻辑
//
con = new SqlConnection(ConfigurationSettings.AppSettings.Get("ConnStr"));
da = new SqlDataAdapter();
}
public DataSet GetStudent()
{
SqlCommand comm = new SqlCommand();
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "Sp_Student";
comm.Connection = con;
da.SelectCommand = comm;
DataSet ds = new DataSet();
try
{
da.Fill(ds);
ds.Tables[0].TableName = "student";
ds.Tables[1].TableName = "cj";
ds.Relations.Add("studcj",ds.Tables["student"].Columns["ID"], ds.Tables["cj"].Columns["S_ID"],false);
}
catch(Exception ex)
{
throw new ApplicationException(ex.ToString());
}
return ds;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(true);
}
protected virtual void Dispose(bool disposing)
{
}
}
}
//Student.cs业务逻辑
using System;
using System.Data;
using DA;
namespace BL
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Student
{
public Student()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public DataTable GetStudent()
{
DataTable table;
using(DA.StudentInf st = new StudentInf())
{
table = st.GetStudent().Tables["student"];
}
return table;
}
}
}
//WebForm1.aspx用户界面
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebUI.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="dg" BorderColor="#33FF33" style="Z-INDEX: 101; LEFT: 256px; POSITION: absolute; TOP: 168px" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<TABLE cellSpacing="0" cellPadding="0" width="100%" border="1" bordercolor="#33ffee">
<TR>
<TD><%# DataBinder.Eval(Container.DataItem, "ID") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "Name") %></td>
<td> </td>
</tr>
<tr><td colspan="2"> </td>
<td>
<asp:DataGrid id=DataGrid2 runat="server" AutoGenerateColumns="False" BorderColor="#33FF33" DataKeyField="ID" DataSource='<%# ((System.Data.DataRowView)(Container.DataItem)).CreateChildView("studcj") %>'>
<Columns>
<asp:BoundColumn DataField="SV" HeaderText="SV"></asp:BoundColumn>
<asp:BoundColumn DataField="Cj" HeaderText="Cj"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></form>
</body>
</HTML>
//WebForm1.aspx.cs后台代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using BL;
namespace WebUI
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dg;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
Bind();
}
}
private void Bind()
{
Student st = new Student();
dg.DataSource = st.GetStudent().DefaultView;
dg.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
lei1217
浙公网安备 33010602011771号