.NET

use Product_Junk

Create table Product
(
Pid int identity primary key,
Jid int,
Typeid int,
Pname varchar(50),
Pnum varchar(50),
Ptime varchar(50),
Pimages varchar(90),
)
--产品类型
create table ProductType
(
Typeid int identity primary key,
Typename varchar(50)
)
--饮品类型
create table Junk
(
Jid int identity primary key,
Junkname varchar(50)
)

--删除
if exists(select * from sys.objects where name='up_delproduct')
drop proc up_delproduct
go
create proc up_delproduct
@id int
as
delete from Product where Pid=@id
go

--批删
if exists(select * from sys.objects where name='up_delsproduct')
drop proc up_delsproduct
go
create proc up_delsproduct
@ids varchar(20)
as
exec ('delete from Product where Pid in('+@ids+')')
go


--分页
if exists(select * from sys.objects where name='up_selproduct')
drop proc up_selproduct
go
CREATE proc up_selproduct
@tableName varchar(8000), --表名、视图名
@indexCol varchar(50) = 'a.id', --标识列名(如:比如主键、标识,推荐使用索引列)
@pageSize int = 10, --页面大小
@pageIndex int = 0, --当前页
@orderCol varchar(100) = 'a.id desc',--排序 (如:id)
@where varchar(max) = '', --条件
@columns varchar(500) = '*' --要显示的列
as
declare @sql varchar(max)
declare @sql2 varchar(max)
declare @where2 varchar(max)

if @where <> ''
begin
select @where2 = ' And ' + @where
select @where = ' Where ' + @where
end
else
select @where2 = ''


select @sql = 'Select Top ' + Convert(varchar(10),@pageSize) + ' ' + @columns + ' From ' + @tableName
select @sql2 = @sql + @where
select @sql = @sql + ' Where ' + '(' + @indexCol + ' Not In (Select Top ' + Convert(varchar(10), ((@pageIndex-1)*@pageSize)) + ' ' + @indexCol + ' From ' + @tableName + @where + ' Order by '+ @orderCol +'))'
select @sql = @sql + @where2
select @sql = @sql + ' Order by ' + @orderCol
--获取数据集
exec (@sql)
PRINT @sql
select @sql2 = Replace(@sql2,'Top ' + Convert(varchar(10), @pageSize) + ' ' + @columns, 'count(1)')
--获取总数据条数
exec(@sql2)

GO


--产品下拉
if exists(select *from sys.objects where name='up_selproducttype')
drop proc up_selproducttype
go
create proc up_selproducttype
as
select * from ProductType
go

--饮品 下拉
if exists(select *from sys.objects where name='up_seljunk')
drop proc up_seljunk
go
create proc up_seljunk
as
select * from Junk
go

--添加
if exists(select *from sys.objects where name='up_seladdproduct')
drop proc up_seladdproduct
go
create proc up_seladdproduct
@Jid int,
@Typeid int,
@Pname varchar(50),
@Pnum varchar(50),
@Ptime varchar(50),
@pimages varchar(90)
as
insert into Product values(@Jid,@Typeid,@Pname,@Pnum,@Ptime,@pimages)
go

 

public string tableName { get; set; }
public string indexCol { get; set; }
public int pageSize { get; set; }
public int pageIndex { get; set; }
public string orderCol { get; set; }
public string where { get; set; }
public string columns { get; set; }

public int tCount { get; set; }
public int tPage { get; set; }
public DataTable tDt { get; set; }

 

//
public int AddProduct(Product model)
{
try
{
SqlParameter[] pars = {new SqlParameter ("@Jid",model.Jid),
new SqlParameter ("@Typeid",model.Typeid),
new SqlParameter ("@Pname",model.Pname),
new SqlParameter ("@Pnum",model.Pnum),
new SqlParameter ("@Ptime",model.Ptime),
new SqlParameter ("@Pimages",model.Pimages),
};
int o = DbHelperSQL.ExecuteNonQuery(DbHelperSQL.ConnB2c, CommandType.StoredProcedure, "up_seladdproduct", pars);
return o;
}
catch (Exception )
{
throw;
}

}
//分页查询
public PageOut SelProduct(PagePut pageput)
{
SqlParameter[] paras = {new SqlParameter ("@tableName",pageput.tableName),
new SqlParameter ("@indexCol",pageput.indexCol),
new SqlParameter ("@pageSize",pageput.pageSize),
new SqlParameter ("@pageIndex",pageput.pageIndex),
new SqlParameter ("@orderCol",pageput.orderCol),
new SqlParameter ("@where",pageput.where),
new SqlParameter ("@columns",pageput.columns),
};
DataSet ds = DbHelperSQL.ExecuteDataset(DbHelperSQL.ConnB2c, CommandType.StoredProcedure, "up_selproduct", paras);
DataTable dt = ds.Tables[0];//获取数据信息
int size = pageput.pageSize;//行数据
int count = Convert.ToInt32(ds.Tables[1].Rows[0][0]);//行 总数
int sumpage = count / size + (count % size > 0 ? 1 : 0);//总页数
PageOut pageout = new PageOut();
pageout.tCount = count;
pageout.tPage = sumpage;
pageout.tDt = dt;
return pageout;
}
//产品 类型
public DataTable SelProductType()
{
return DbHelperSQL.ExecuteDataTable(DbHelperSQL.ConnB2c, CommandType.StoredProcedure, "up_selproducttype", null);
}
//饮品 类型
public DataTable Seljunk()
{
return DbHelperSQL.ExecuteDataTable(DbHelperSQL.ConnB2c, CommandType.StoredProcedure, "up_seljunk", null);
}
//删除
public int DelProduct(int id)
{
SqlParameter[] pars = { new SqlParameter("@id", id) };
return DbHelperSQL.ExecuteNonQuery(DbHelperSQL.ConnB2c, CommandType.StoredProcedure, "up_delproduct", pars);
}
//批量删除
public int DelsProduct(string ids)
{
SqlParameter[] pars = { new SqlParameter("@ids", ids) };
return DbHelperSQL.ExecuteNonQuery(DbHelperSQL.ConnB2c, CommandType.StoredProcedure, "up_delsproduct", pars);
}

//

/// <summary>
/// 实例化 dal
/// </summary>
Service se = new Service();
public int AddProduct(Product model)
{
return se.AddProduct(model);

}
public PageOut SelProduct(PagePut json)
{

return se.SelProduct(json);
}

public DataTable SelProductType()
{
return se.SelProductType();
}
public DataTable Seljunk()
{
return se.Seljunk();
}

public int DelProduct(int id)
{

return se.DelProduct(id);
}
public int DelsProduct(string ids)
{
return se.DelsProduct(ids);
}

//

Manager man = new Manager();
static int size = 3;
static int index = 1;
static int count = 0;
static int page = 0;
string Where = "";
static string orderCol = "a.Pid asc";
/// <summary>
/// 分页显示 查询
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
SelProductType();
SelJunk();
List<ProductList> list = ShowPage();
return View(list);
}
//查询
[HttpPost]
public ActionResult Index(string Jid, string Typeid, string Name)
{
SelProductType();
SelJunk();
string where = "1=1";
if (!String.IsNullOrEmpty(Jid))
{
where += "and a.Jid=" + Jid;
}
if (!String.IsNullOrEmpty(Typeid))
{
where += "and a.Typeid=" + Typeid;
}
if (!String.IsNullOrEmpty(Name))
{
where += "and a.Pname like'%" + Name + "'";
}
Where = where;
List<ProductList> list = ShowPage();
return View("Index", list);

}
//首页
public ActionResult First()
{
SelJunk();
SelProductType();
index = 1;
List<ProductList> list = ShowPage();
return View("Index", list);
}

//上一页
public ActionResult Shang()
{

SelProductType();
SelJunk();
if (index > 1)
{
index--;
}
List<ProductList> list = ShowPage();
return View("Index", list);
}
//下一页
public ActionResult Next()
{
SelJunk();
SelProductType();
if (index < page)
{
index++;
}
List<ProductList> list = ShowPage();
return View("Index", list);
}
//尾页
public ActionResult Last()
{
SelJunk();
SelProductType();
List<ProductList> list = ShowPage();
return View("Index", list);
}
//删除
public string DelProduct(int Pid)
{
if (man.DelProduct(Pid) > 0)
{
return "<script>alert('删除成功');location.href='/Product/Index/'</script>";
}
else
{
return "<script>alert('删除失败');location.href='/Product/Index/'</script>";
}
}
//批量删除
public int DelsProduct(string ids)
{
string id = ids.TrimEnd(',');
if (man.DelsProduct(id) > 0)
{
return 1;
}
else
{
return 0;
}
}

public ActionResult AddProduct()
{
SelJunk();
SelProductType();
return View();
}
[HttpPost]
public JsonResult AddProduct(Viewmodel model)
{
SelJunk();
SelProductType();
if (model.Pimgurl != null)
{
string path = Path.Combine(Request.MapPath("/Content/"), model.Pimgurl.FileName);
model.Pimgurl.SaveAs(path);
}
Product list = new Product()
{
Jid = model.Jid,
Typeid = model.Typeid,
Pname = model.Pname,
Pnum = model.Pnum,
Ptime = model.Ptime,
Pimages = "/Content/" + model.Pimgurl.FileName
};
int i = man.AddProduct(list);
return Json(i);
}

//封装数据
public List<ProductList> ShowPage()
{
//输入参数
PagePut pageput = new PagePut();
string tableName = "Product a inner join Junk b on a.Jid=b.Jid inner join ProductType c on a.Typeid=c.Typeid";
pageput.tableName = tableName;
pageput.indexCol = "a.Pid";
pageput.pageSize = size;
pageput.pageIndex = index;
pageput.orderCol = orderCol;
pageput.where = Where;
pageput.columns = "*";

//输出 参数
PageOut pageout = man.SelProduct(pageput);
count = pageout.tCount;//总条数
page = pageout.tCount;//总页数

ViewBag.count = count;
ViewBag.page = page;
ViewBag.inde = index;
List<ProductList> list = JsonConvert.DeserializeObject<List<ProductList>>(JsonConvert.SerializeObject(pageout.tDt));
return list;

}
//产品类型
public void SelProductType()
{
DataTable dt = man.SelProductType();
List<ProductType> list = JsonConvert.DeserializeObject<List<ProductType>>(JsonConvert.SerializeObject(dt));
var linq = from s in list
select new SelectListItem
{
Text = s.Typename,
Value = s.Typeid.ToString()
};
ViewBag.SelProductType = linq.ToList();
}
//饮品类型
public void SelJunk()
{
DataTable dt = man.Seljunk();
List<Junk> list = JsonConvert.DeserializeObject<List<Junk>>(JsonConvert.SerializeObject(dt));
var linq = from s in list
select new SelectListItem
{
Text = s.Junkname,
Value = s.Jid.ToString()
};
ViewBag.SelJunk = linq.ToList();
}

 

 

///

@model List<Model.ProductList>

<script>

function QuanXuan() {
$("[name='cks']").prop("checked", $("#QuanXuan").prop("checked"))
}
function FanXuan() {
$("[name='cks']").each(function () {
$(this).prop("checked", !$(this).prop("checked"))
})
}
//批量删除
function Dels() {
var str = "";
$("[name='cks']:checked").each(function () {
str+= this.value+ ",";
})
$.ajax({
url:"/Product/DelsProduct/",
data: { ids: str},
type: "post",
success: function (data) {
if (data > 0) {
alert("删除成功");
location.href = '/Product/Index/';

}
}

})
}
</script>

<div>
@using (Html.BeginForm("Index", "Product", FormMethod.Post))
{
<span>产品 类型 </span>@Html.DropDownList("Typeid", ViewBag.SelProductType as List<SelectListItem>,"==请选择==")
<span>饮品 类型 </span>@Html.DropDownList("Jid", ViewBag.Seljunk as List<SelectListItem>,"==请选择==")
<input type="text" name="Name" placeholder="产品名称" />
<input type="submit" value="查询">
<input id="Button1" type="button" value="添加数据" οnclick="location.href='/Product/AddProduct'">
<input id="Button2" type="button" value="批量删除" οnclick="Dels()">

}
<table class="table table-bordered">
<tr>
<th>
全选
<input id="QuanXuan" type="checkbox" name="QuanXuan" οnclick="QuanXuan()" />
反选
<input id="FanXuan" type="checkbox" name="FanXuan" οnclick="FanXuan()" />
</th>
<th>编号</th>
<th>产品名称</th>
<th>产品数量</th>
<th>产品时间 </th>
<th>图片</th>
<th>产品种类</th>
<th>饮品种类</th>
<th>操作</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
<input id="cks" type="checkbox" name="cks" value="@item.Pid" />
</td>
<td>@item.Pid</td>
<td>@item.Pname</td>
<td>@item.Pnum</td>
<td>@item.Ptime</td>
<td><img src="@item.Pimages" alt="" style="width:50px;height:50px;" /></td>
<td>@item.Typename</td>
<td>@item.Junkname</td>
<td>@Html.ActionLink("删除","DelProduct",new { item.Pid})</td>
</tr>
}
</table>
<p>共 <span style="color:#ff0000">@ViewBag.count</span> 条数据 共计 <span style="color:#ff0000">@ViewBag.page</span> 页 当前第 <span style="color:#ff0000">@ViewBag.index</span> 页</p>
<p style=" padding-left:150px;">
@Html.ActionLink("首页", "First")
@Html.ActionLink("上一页", "Shang")
@Html.ActionLink("下一页", "Next")
@Html.ActionLink("尾页", "Last")
</p>
</div>

 

//

@model MVC_Product_junk.Models.Viewmodel

 

<script>
function addform() {
$("#form0").ajaxSubmit({
type: "post",
url: "/Product/AddProduct/",
success: function (data) {
if (data > 0)
{
alert("添加成功");
location.href = '/Product/Index/';

}
}
})
}
function Out()
{
location.href = '/Product/Index/';
}
</script>

<div>
@using (Ajax.BeginForm(new AjaxOptions { }))
{

<table>
<tr>
<td>产品类型</td>
<td>@Html.DropDownListFor(m => m.Typeid, ViewBag.SelProductType as List<SelectListItem>, "==请选择==")</td>
</tr>
<tr>
<td>饮品类型</td>
<td>@Html.DropDownListFor(m => m.Jid, ViewBag.SelJunk as List<SelectListItem>, "==请选择==")</td>
</tr>
<tr>
<td>产品名称</td>
<td>@Html.TextBoxFor(m => m.Pname)</td>
</tr>
<tr>
<td>产品数量</td>
<td>@Html.TextBoxFor(m => m.Pnum)<span></span></td>
</tr>

<tr>
<td>生产日期</td>
<td>@Html.TextBoxFor(m => m.Ptime)</td>
</tr>
<tr>
<td>图片</td>
<td>@Html.TextBoxFor(m => m.Pimgurl, new { @type = "file" })</td>
</tr>
<tr>
<td></td>
<td>
<input id="Submit1" type="button" value="提交" οnclick="addform()" />
<input id="Submit1" type="button" value="取消" οnclick="Out()" />
</td>
</tr>
</table>

}

</div>

 

/

BasicDataFactoty

 

using System.Configuration;
using System.Reflection;
namespace Factory
{
public class BasicDataFactoty<T>
{
static string Typenamee = ConfigurationManager.AppSettings["DBName"].ToString();

public static T CreateFactory(string typename)
{


Assembly ass = Assembly.Load(typename);
T t =(T)ass.CreateInstance(Typenamee + "." + typename);
return t;
}
}
}

转载于:https://www.cnblogs.com/0901lkx/p/9386396.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值