个人任务管理系统复习(数据库,MODEL,DAL)

本文回顾了一个个人任务管理系统的实现,涵盖了数据库设计、MODEL层的定义以及DAL层的操作,使用了SqlHelper作为数据访问层的辅助工具。
摘要由CSDN通过智能技术生成

数据库

use master
go

if exists (select * from sys.databases where name='Test')
drop database Test
go

create database Test
go

use Test
go

create table Project
(
ProjectID int primary key identity(1,1), --主键,自动增长列项目编号
ProjectName varchar(50) not null --不允许为空项目名称
)
create table Task
(
TaskID int primary key identity(1,1),
TaskPriority int,
Content varchar(200) not null,
AddTime datetime default( getdate()),
TaskState int default(1),
FinishedTime datetime default(null),
ProjectID int references Project(ProjectID)
)
go

insert into Project values('考勤管理')
insert into Project values('申请管理')
insert into Project values('会员管理')


insert into Task values (1,'进行1',getdate(),default,default,1)
insert into Task values (2,'进行2',getdate(),2,default,3)
insert into Task values (3,'进行3',getdate(),2,default,1)
insert into Task values (2,'进行4',getdate(),default,default,2)
select Task.*,Project.ProjectName from Project,Task where  Project.ProjectID=Task.ProjectID

MODEL层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace MODEL
{
    public class Task
    {
        public int TaskID { get; set; }
        public int TaskPriority { get; set; }
        public string Content { get; set; }
        public DateTime AddTime { get; set; }
        public int TaskState { get; set; }
        public DateTime FinishedTime { get; set; }
        public int ProjectID { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MODEL
{
    public class Project
    {
        /// <summary>
        /// 自动增长列项目编号
        /// </summary>
        public int ProjectID { get; set; }
        /// <summary>
        /// 不允许为空项目名称
        /// </summary>
        public string ProjectName { get; set; }
    }
}

DAL层

SqlHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
    public class SqlHelper
    {
        private static string strconn = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;

        public static DataTable Query(string sql)
        {
            SqlDataAdapter sda = new SqlDataAdapter(sql,strconn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            return dt;
        }

        public static int NonQuery(string sql) {

            int num = 0;
            SqlConnection conn = new SqlConnection(strconn);
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                num = cmd.ExecuteNonQuery();
            }
            finally
            {
                if (conn.State==ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            return num;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MODEL;

namespace DAL
{
    public class Task_DAL
    {
        //查询
        public static DataTable Select(int id = 0, string name = "", int tid = 0)
        {
            string sql = "select Task.*,Project.ProjectName from Project,Task where  Project.ProjectID=Task.ProjectID";
            if (id != 0)
            {
                sql += "  and Task.ProjectID=" + id;
            }
            if (name != "")
            {
                sql += "  and Content like '%" + name + "%'";
            }
            if (tid != 0)
            {
                sql += "  and TaskState=" + tid;
            }

            return SqlHelper.Query(sql);
        }

        //更新
        public static int Update(int id)
        {
            string sql = "update task set FinishedTime=getdate(),TaskState=2 where TaskID=" + id;
            return SqlHelper.NonQuery(sql);
        }

        //删除
        public static int Delete(int id)
        {
            string sql = "delete from task where TaskID=" + id;
            return SqlHelper.NonQuery(sql);
        }

        //插入
        public static int Insert(Task biao)
        {
            string sql = string.Format("insert into Task values ({0},'{1}',getdate(),default,default,{2})",biao.TaskPriority,biao.Content,biao.ProjectID);
            return SqlHelper.NonQuery(sql);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MODEL;

namespace DAL
{
    public class Project_DAL
    {
        //查询
        public static DataTable Select()
        {
            string sql = "select * from Project";
            return SqlHelper.Query(sql);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值