首页 > 代码库 > oa_mvc_easyui_(1)

oa_mvc_easyui_(1)

1.空项目的搭建,三层的搭建(各层之中的引用)

  webapp:bll,model,common

  bll:dal,model

  dal:model

技术分享

 

2.SQL表

ItcastDb:T_UserInfo,T_News,T_NewComments

T_UserInfo:

USE [ItcastDb]
GO

/****** Object:  Table [dbo].[T_UserInfo]    Script Date: 05/24/2017 09:13:43 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[T_UserInfo](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [UserName] [nvarchar](50) NULL,
    [UserPwd] [nvarchar](50) NULL,
    [UserMail] [nvarchar](50) NULL,
    [RegTime] [datetime] NULL,
 CONSTRAINT [PK_T_UserInfo] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[T_UserInfo] ADD  CONSTRAINT [DF_T_UserInfo_RegTime]  DEFAULT (getdate()) FOR [RegTime]
GO

T_News:

USE [ItcastDb]
GO

/****** Object:  Table [dbo].[T_News]    Script Date: 05/24/2017 09:13:38 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[T_News](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [Title] [nvarchar](50) NULL,
    [Msg] [nvarchar](2000) NULL,
    [SubDateTime] [datetime] NULL,
    [Author] [nvarchar](50) NULL,
    [ImagePath] [nvarchar](50) NULL,
 CONSTRAINT [PK_T_News] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

T_NewComments:

USE [ItcastDb]
GO

/****** Object:  Table [dbo].[T_NewComments]    Script Date: 05/24/2017 09:13:28 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[T_NewComments](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [NewId] [int] NULL,
    [Msg] [nvarchar](50) NULL,
    [CreateDateTime] [datetime] NULL,
 CONSTRAINT [PK_T_NewComments] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

 

3.Model实体类的编写

T_NewComments:

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Itcast.CMS.Model
{
    public class T_NewComments
    {
        public int Id { get; set; }
        public int NewId { get; set; }
        public string Msg { get; set; }
        public DateTime CreateDateTime { get; set; }
    }
}
View Code

T_UserInfo:

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Itcast.CMS.Model
{
    public class T_UserInfo
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public string UserPwd { get; set; }
        public string UserMail { get; set; }
        public DateTime RegTime { get; set; }
    }
}
View Code

T_News:

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Itcast.CMS.Model
{
    public class T_News
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Msg { get; set; }
        public DateTime SubDateTime { get; set; }
        public string Author { get; set; }
        public string ImagePath { get; set; }
    }
}
View Code

 

4.DAl层编写SqlHelp类

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace Itcast.CMS.DAL
{
    public class SqlHelper
    {
        /// <summary>
        /// 自定义数据库连接字符串
        /// </summary>
        public static string ConnString = @"Data Source=.;Initial Catalog=zhigongWebDB; User ID=sa;Pwd=123456";
        /// <summary>
        /// 应用程序下获取连接字符串
        /// </summary>
        //public static string ConnString
        //{
        //    get {

        //        return System.Configuration.ConfigurationManager.ConnectionStrings["connstring"].ToString();
        //    }
        //}

        /// <summary>
        /// 执行一个Sql语句返回受影响的行数
        /// </summary>
        /// <param name="sql">insert,update,delete或相关的存储过程</param>
        /// <param name="type">命令类型:SQL语句还是存储过程</param>
        /// <param name="pars">SQL语句所需要的参数</param>
        public static int ExcuteSQLReturnInt(string sql, CommandType type, params SqlParameter[] pars)
        {
            //定义连接对象
            SqlConnection conn = new SqlConnection(ConnString);
            //判断连接对象的状态,并且打开
            if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
            {
                conn.Open();
            }
            try
            {
                //实例化命令对象
                SqlCommand cmd = new SqlCommand(sql, conn);
                //判断CommandType类型是否是SQL语句还是存储过程
                cmd.CommandType = type;
                if (pars != null && pars.Length > 0)
                {
                    foreach (SqlParameter p in pars)
                    {
                        cmd.Parameters.Add(p);
                    }
                }
                //调用方法执行SQL语句或存储过程
                int count = cmd.ExecuteNonQuery();
                return count;
            }
            catch (Exception ex)
            {
                return 0;
            }
            finally
            {
                //记得要关闭连接
                conn.Close();
            }
        }

        /// <summary>
        /// 执行一个Sql语句或存储过程,返回一条记录,sqldataReader
        /// </summary>
        /// <param name="sql">select语句,或相关的存储过程</param>
        /// <param name="type">指定命令类型,sql语句还是存储过程,默认的是sql语句</param>
        /// <param name="pars">参数的集合</param>
        /// <returns></returns>
        public static SqlDataReader ExcuteSqlReturnReader(string sql, CommandType type, SqlParameter[] pars)
        {
            SqlConnection conn = new SqlConnection(ConnString);
            if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand(sql, conn);
            if (pars != null && pars.Length > 0)
            {
                foreach (SqlParameter p in pars)
                {
                    cmd.Parameters.Add(p);
                }
            }
            cmd.CommandType = type;
            //当reader.close(),也就是关闭了datareader时,CommandBehavior.CloseConnection 也会关闭连接!
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
        }

        /// <summary>
        /// 执行一个sql语句或存储过程,不带参数的!返回一天记录 sqldataReader
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static SqlDataReader ExcuteSqlReturnReader(string sql, CommandType type)
        {
            SqlConnection conn = new SqlConnection(ConnString);
            if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = type;
            //当reader.close(),也就是关闭了datareader时,CommandBehavior.CloseConnection 也会关闭连接!
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
        }

        /// <summary>
        ///  执行一个Sql语句或存储过程,返回一个数据集,dataset
        /// </summary>
        /// <param name="sql">select语句或相关的存储过程</param>
        /// <param name="type">命令类型</param>
        /// <param name="pars">命令类型</param>
        /// <returns>DataSet</returns>
        public static DataSet SelectSqlReturnDataset(string sql, CommandType type, SqlParameter[] pars)
        {
            SqlConnection conn = new SqlConnection(ConnString);
            //数据集识别器对象SqlDataAdapter 会自动打开数据库链接!
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            if (pars != null && pars.Length > 0)
            {
                foreach (SqlParameter p in pars)
                {
                    sda.SelectCommand.Parameters.Add(p);
                }
            }
            sda.SelectCommand.CommandType = type;
            DataSet ds = new DataSet();
            sda.Fill(ds);
            return ds;
        }

        /// <summary>
        /// 执行一个sql语句返回一个数据表对象
        /// </summary>
        /// <param name="sql">select</param>
        /// <param name="type">命令类型</param>
        /// <param name="pars">参数集合</param>
        /// <returns>DataTable</returns>
        public static DataTable SelectSqlReturnDataTable(string sql, CommandType type, SqlParameter[] pars)
        {
            SqlConnection conn = new SqlConnection(ConnString);
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            if (pars != null && pars.Length > 0)
            {
                foreach (SqlParameter p in pars)
                {
                    sda.SelectCommand.Parameters.Add(p);
                }
            }
            sda.SelectCommand.CommandType = type;
            DataTable dt = new DataTable();
            sda.Fill(dt);
            return dt;
        }

        //*******************************************************************
        //SelectSqlReturnDataTable方法的重载,不传参数的情况下
        /// <summary>
        /// 执行一个sql语句返回一个数据表对象,不传参数
        /// </summary>
        /// <param name="sql">select</param>
        /// <param name="type">命令类型</param>
        /// <returns></returns>
        public static DataTable SelectSqlReturnDataTable(string sql, CommandType type)
        {
            SqlConnection conn = new SqlConnection(ConnString);
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            sda.SelectCommand.CommandType = type;
            DataTable dt = new DataTable();
            sda.Fill(dt);
            return dt;
        }

        /// <summary>
        /// 执行一个sql语句或相关的存储过程,返回一个值
        /// </summary>
        /// <param name="sql">select count(*) from tablename where ....</param>
        /// <param name="type">命令类型</param>
        /// <param name="pars">所需的参数</param>
        /// <returns>object</returns>
        public static object selectSqlReturnObject(string sql, CommandType type, SqlParameter[] pars)
        {
            SqlConnection conn = new SqlConnection(ConnString);
            if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
            {
                conn.Open();
            }
            try
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandType = type;
                if (pars != null && pars.Length > 0)
                {
                    foreach (SqlParameter p in pars)
                    {
                        cmd.Parameters.Add(p);
                    }
                }
                object obj = cmd.ExecuteScalar();
                return obj;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                conn.Close();
            }
        }

    }
}
View Code

 

5.新建登录控制器(LoginControllers),并添加index视图

技术分享

技术分享

**更改路由:Global.asax--RouteConfig.RegisterRoutes(RouteTable.Routes)

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Itcast.CMS.WebApp
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}
View Code

 

oa_mvc_easyui_(1)