序言
您好! 这是我第一次发表文章。闲话少叙,在我踏上学习C#这条一路以后,作为一名新手,作为女生,不后悔。我的技术水平目前约为一渣渣,各位您请批评指教。如有兴趣,可阅读本篇文章,大略了解一下我的第一天记录。
开始
☆工具说明:
数据库:Microsoft SQL Server Management Studio 17
编码:启动 Visual Studio 2017 的实验实例
MVC
ADO
1数据表
## 1.1分类表
create table ProductClass–商品分类表
(
ClassID int constraint ProductClass_PK primary key identity(1,1),–主键,自增,商品分类ID
ClassName nvarchar(50) not null,–分类名称
ParentID int default 0 not null,–父分类ID
ParentPath nvarchar(500) not null–分类路径
)
## 1.2插入数据
insert into ProductClass(ClassName,ParentID,ParentPath) values(‘权限设置’,0,‘0’),(‘角色管理’,1,‘0,1’),(‘角色管理’,2,‘0,1,2’),(‘角色添加’,2,‘0,1,2’),(‘角色添加’,2,‘0,1,2’),(‘角色修改’,2,‘0,1,2’),(‘角色删除’,2,‘0,1,2’),(‘角色查询’,2,‘0,1,2’),(‘用户管理’,1,‘0,2’),(用户管理,9,‘0,2,9’),
(用户添加,9,‘0,2,9’),(用户修改,9,‘0,2,9’),(用户删除,9,‘0,2,9’),(日志管理,1,‘0,3’),(日志存储,14,‘0,3,14’),(日志销毁,14,‘0,3,14’)……
select count(*) from ProductClass
select * from ProductClass
2.VS Code
## 2.1 Model 模型
public class ProductClass
{
public int ClassID { get; set; }
public string ClassName { get; set; }
public int ParentID { get; set; }
public string ParentPath { get; set; }
}
## 2.2 Controller 控制器
引用一下相应的名称空间,开始写代码(CRUD)
//数据库连接字符串
string str = "Data Source=.;Initial Catalog=Vocation;Integrated Security=True";
//实例化
List<ProductClass> clist = new List<ProductClass>();
//视图
public ActionResult Index()
{
GetClass(1);
return View(clist);
}
//构造函数:初始化数据
public ProductsController()
{
using (SqlConnection conn = new SqlConnection(str))
{
SqlDataAdapter sdr = new SqlDataAdapter("SELECT * FROM ProductClass WHERE ParentID=0", conn);
DataTable dt = new DataTable();
sdr.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
clist.Add(new ProductClass { ClassID = (int)dr["ClassID"], ClassName = dr["ClassName"].ToString(), ParentID = (int)dr["ParentID"], ParentPath = dr["ParentPath"].ToString() });
GetClass((int)dr["ClassID"]);
}
}
}
//获取类型信息
public void GetClass(int pid)
{
using (SqlConnection conn=new SqlConnection(str))
{
string str = string.Format("SELECT * FROM ProductClass WHERE ParentID={0}", pid);
SqlCommand cmd = new SqlCommand(str, conn);
cmd.Parameters.AddWithValue("@ParentID", pid);
SqlDataAdapter sdr = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sdr.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
clist.Add(new ProductClass { ClassID=(int)dr["ClassID"] , ClassName=dr["ClassName"].ToString(), ParentID=(int)dr["ParentID"], ParentPath=dr["ParentPath"].ToString() });
GetClass((int)dr["ClassID"]);
}
}
}
//删除
public int DelClass(int cid)
{
using (SqlConnection conn = new SqlConnection(str))
{
conn.Open();
string str = string.Format("DELETE FROM ProductClass WHERE ClassID={0}",cid);
SqlCommand cmd = new SqlCommand(str, conn);
return cmd.ExecuteNonQuery();
}
}
//添加或修改
public int AddOrUpdClass(ProductClass pc)
{
using (SqlConnection conn = new SqlConnection(str))
{
conn.Open();
string str = "";
if (pc.ClassID>0)
{
str = string.Format("UPDATE ProductClass SET ClassName='{0}',ParentID={1},ParentPath='{2}'", pc.ClassName,pc.ParentID,pc.ParentPath);
}
else
{
str = string.Format("INSERT INTO ProductClass VALUES('{0}',{1},'{2}')",pc.ClassName,pc.ParentID,pc.ParentPath);
}
SqlCommand cmd = new SqlCommand(str, conn);
return cmd.ExecuteNonQuery();
}
}
//反填
public ActionResult GetClassById(int id)
{
using (SqlConnection conn = new SqlConnection(str))
{
string str = string.Format("SELECT * FROM ProductClass WHERE ClassID={0}", id);
DataTable dt = new DataTable();
SqlDataAdapter sdr = new SqlDataAdapter();
sdr.Fill(dt);
ProductClass pmode = new ProductClass();
pmode = JsonConvert.DeserializeObject<List<ProductClass>>(JsonConvert.SerializeObject(dt)).First();
return Json(pmode,JsonRequestBehavior.AllowGet);
}
}
## 2.3 View 视图
根据Action渲染View, 只附上显示的视图
@model IEnumerable<ProductsPlanLibingyang.Models.ProductClass>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@foreach (var p in Model)
{
if (p.ParentID != 0)
{
string str = string.Empty;
int level = p.ParentPath.Split(',').Length;
for (int i = 0; i < level; i++)
{
str += " ";
}
@Html.Raw(str+ "|—")
}
@p.ClassName<br />
}
运行效果附上:
如图所示
3.汇总 :
主要是递归的思想一定要把握!
但我发现:呀嘿, 难道是 我的 子节点数据 重复加载了?
调整了一下:
还是好奇怪。
##有始有终:目前只是出于学习状态,也只是记录一下自己所学的东西。毕竟咱不是大神,一直在努力,对自己说一声:加油吧!
无力吐槽了,歇一会儿吧。。。
如有机会,下次再记录。