public static void Add(ProductInfo dal)
{
string sql = "insert into Product(productName,unitprice,special,categoryId) values(@productName,@unitprice,@special,@categoryId)";
SqlConnection con = new DBConnection().Con;
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = sql;
//配参数
cmd.Parameters.Add(new SqlParameter("@productName",dal.ProductName));
cmd.Parameters.Add(new SqlParameter("@unitprice",dal.Unitprice));
cmd.Parameters.Add(new SqlParameter("@special", dal.Special));
cmd.Parameters.Add(new SqlParameter("@categoryId", dal.CategoryId));
con.Open();
try
{
cmd.ExecuteNonQuery();
}
finally {
con.Close();
}
}
public static void Update(ProductInfo dal)
{
string sql = "update Product set productName=@productName,unitprice=@unitprice,special=@special,categoryId=@categoryId where productId=@productId";
SqlConnection con = new DBConnection().Con;
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = sql;
//配参数
cmd.Parameters.Add(new SqlParameter("@productName", dal.ProductName));
cmd.Parameters.Add(new SqlParameter("@unitprice", dal.Unitprice));
cmd.Parameters.Add(new SqlParameter("@special", dal.Special));
cmd.Parameters.Add(new SqlParameter("@categoryId", dal.CategoryId));
cmd.Parameters.Add(new SqlParameter("@productId", dal.ProductId));
con.Open();
try
{
cmd.ExecuteNonQuery();
}
finally
{
con.Close();
}
}
public static void Delete(int productId)
{
string sql = "delete Product where productId=@productId";
SqlConnection con = new DBConnection().Con;
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = sql;
//配参数
cmd.Parameters.Add(new SqlParameter("@productId", productId));
con.Open();
try
{
cmd.ExecuteNonQuery();
}
finally
{
con.Close();
}
}
public ProductOper()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
}
,8
/Default.aspx
复制代码 代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<div>
<asp:HyperLink runat="server" Text="添加" NavigateUrl="Create.aspx"></asp:HyperLink>
<asp:GridView runat="server" AutoGenerateColumns="False"
onrowcancelingedit="gvwProduct_RowCancelingEdit"
onrowdatabound="gvwProduct_RowDataBound" onrowdeleting="gvwProduct_RowDeleting"
onrowediting="gvwProduct_RowEditing"
onrowupdating="gvwProduct_RowUpdating" AllowPaging="True"
onpageindexchanging="gvwProduct_PageIndexChanging" PageSize="5">
<Columns>
<asp:TemplateField HeaderText="产品编号">
<EditItemTemplate>
<asp:Label runat="server" Text='<%# Bind("productId") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("productId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="产品名称">
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# Bind("productName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("productName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="单价">
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# Bind("unitprice") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("unitprice") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="是否特价">
<EditItemTemplate>
<asp:RadioButtonList runat="server"
RepeatDirection="Horizontal" RepeatLayout="Flow">
<asp:ListItem>特价</asp:ListItem>
<asp:ListItem>非特价</asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("special") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="类别编号">
<EditItemTemplate>
<asp:DropDownList runat="server">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("categoryId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
/Default.aspx.cs
复制代码 代码如下:
using System;
using System.Web.UI.WebControls;