asp.net中gridview的查询、分页、编辑更新、删除的实例代码

时间:2021-05-28

1.A,运行效果图

1.B,源代码
/App_Data/sql-basic.sql
复制代码 代码如下:
use master
go
if exists(select * from sysdatabases where name='db1')
begin
drop database db1
end
go
create database db1
go
use db1
go
-- ================================
-- ylb:1,类别表
-- ================================
create table category
(
categoryid int identity(1,1) primary key, --编号【PK】
categoryname varchar(20) not null --名称
)

insert into category(categoryname) values('饮料')
insert into category(categoryname) values('主食')
insert into category(categoryname) values('副食')
insert into category(categoryname) values('蔬菜')

-- ================================
-- ylb:2,产品表
-- ================================
create table product
(
productid int identity(1001,1) primary key, --编号【PK】
productname varchar(20), --名称
unitprice numeric(7,2), --单价
special varchar(10) check(special in('特价','非特价')), --是否特价【C】
categoryid int foreign key references category(categoryid) --类别编号【FK】
)

insert into product(productname,unitprice,special,categoryid) values('可乐1',12.6,'特价',1)
insert into product(productname,unitprice,special,categoryid) values('可乐2',12.6,'非特价',1)
insert into product(productname,unitprice,special,categoryid) values('可乐3',12.6,'非特价',1)
insert into product(productname,unitprice,special,categoryid) values('可乐4',12.6,'非特价',1)
insert into product(productname,unitprice,special,categoryid) values('可乐5',12.6,'特价',1)
insert into product(productname,unitprice,special,categoryid) values('可乐6',12.6,'特价',1)
insert into product(productname,unitprice,special,categoryid) values('可乐7',12.6,'特价',1)
insert into product(productname,unitprice,special,categoryid) values('可乐8',12.6,'特价',1)
insert into product(productname,unitprice,special,categoryid) values('馒头1',12.6,'特价',2)
insert into product(productname,unitprice,special,categoryid) values('豆腐1',12.6,'特价',3)
insert into product(productname,unitprice,special,categoryid) values('冬瓜1',12.6,'特价',4)

select * from category
select productid,productname,unitprice,special,categoryid from product

,2
/App_Code/
/App_Code/DBConnection.cs

复制代码 代码如下:
using System.Data.SqlClient;
/// <summary>
///DBConnection 的摘要说明
///数据连接类
/// </summary>
public class DBConnection
{
SqlConnection con = null;

public DBConnection()
{
//创建连接对象
con = new SqlConnection("Server=.;Database=db1;Uid=sa;pwd=sa");
}

/// <summary>
/// 数据连接对象
/// </summary>
public SqlConnection Con
{
get { return con; }
set { con = value; }
}
}

/App_Code/CategoryInfo.cs
/App_Code/CategoryOper.cs
/App_Code/ProductInfo.cs

复制代码 代码如下:
using System;

/// <summary>
///ProductInfo 的摘要说明
///产品实体类
/// </summary>
public class ProductInfo
{
//1,Attributes
int productId;
string productName;
decimal unitprice;
string special;
int categoryId;

public ProductInfo()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
//3,

/// <summary>
/// 产品编号【PK】
/// </summary>
public int ProductId
{
get { return productId; }
set { productId = value; }
}
/// <summary>
/// 产品名称
/// </summary>
public string ProductName
{
get { return productName; }
set { productName = value; }
}
/// <summary>
/// 单位价格
/// </summary>
public decimal Unitprice
{
get { return unitprice; }
set { unitprice = value; }
}
/// <summary>
/// 是否为特价【C】(特价、非特价)
/// </summary>
public string Special
{
get { return special; }
set { special = value; }
}
/// <summary>
/// 类编编号【FK】
/// </summary>
public int CategoryId
{
get { return categoryId; }
set { categoryId = value; }
}
}

/App_Code/ProductOper.cs

复制代码 代码如下:
using System;
using System.Collections.Generic;

using System.Data.SqlClient;
/// <summary>
///ProductOper 的摘要说明
/// </summary>
public class ProductOper
{
/// <summary>
/// 1,GetAll
/// </summary>
/// <returns></returns>
public static IList<ProductInfo> GetAll()
{
IList<ProductInfo> dals = new List<ProductInfo>();
string sql = "select productId,productName,unitprice,special,categoryId from Product order by productId desc";

//1,创建连接对象
SqlConnection con = new DBConnection().Con;
//2,创建命令对象
SqlCommand cmd = con.CreateCommand();

//3,把sql语句付给命令对象
cmd.CommandText = sql;

//4,打开数据连接
con.Open();
try
{
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
ProductInfo dal = new ProductInfo()
{
ProductId = sdr.GetInt32(0),
ProductName = sdr.GetString(1),
Unitprice = sdr.GetDecimal(2),
Special = sdr.GetString(3),
CategoryId = sdr.GetInt32(4)
};

dals.Add(dal);
}
}
}
finally
{
//,关闭数据连接(释放资源)
con.Close();
}
return dals;
}

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:///

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章