Create Table Products(
ProductID int identity(1,1) Primary key,
ProductName nvarchar(100) not null,
CatalogName nvarchar(50) not null
)我们用工具为他生成了基本的操作语句:
Select * FROM Products Where @condition
Insert INTO Products(ProductName,CatalogName) VALUES(@ProductName,@CatalogName)
Update Products SET ProductName=@ProductName,CatalogName=@CatalogName Where ProductID=@ProductID
Delete FROM Products Where ProductID=@ProductID但在实际运用中,通常不是把CatalogName直接放在Products表中的,Products表中只存放CatalogID的值,表结构如下:
Create Table Catalog(
CatalogID int identity(1,1) Primary key,
CatalogName nvarchar(100) not null
)
Create Table Products(
ProductID int identity(1,1) Primary key,
ProductName nvarchar(100) not null,
CatalogID int /*指向Catalog表的CatalogID*/
)同样也用工具生成了基本的操作语句:
Select * FROM Products Where @condition
Insert INTO Products(ProductName,CatalogID) VALUES(@ProductName,@CatalogID)
Update Products SET ProductName=@ProductName,CatalogID=@CatalogID Where ProductID=@ProductID
Delete FROM Products Where ProductID=@ProductID但在使用中有个问题,我们希望在用户界面上显示CatalogName,而不是CatalogID,这时我们很"聪明"的想到,多表查询嘛,很简单:
Select a.@ProductID,a.ProductName,b.CatalogName FROM Products a LEFT JOIN Catalog b ON a.CatalogID=b.CatalogID这只是个简单的例子,实际项目中很多表间的字段都是关联的,于是自动化工具的恶梦就来了,不论他怎么智能,永远满足不了需求,只好手工写了,我们又成功的理由充分的说服自己成了代码机器-_-
要自动化就必须简单化,上边例子还是用工具生成各表的基本操作语句,对于CatalogName,可在需要时再调用Catalog表的操作语句另外进行查询:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" />
<asp:TemplateField HeaderText="CatalogName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# [color=Red]GetCatalogName(Eval("CatalogID"))[/color] %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
评论(0)
暂无评论。