说明
自本文发布日起,发现和完善了编写的CodeSmith几处代码。
故,不再直接在文章添加代码,最新代码参见GitHub
https://github.com/catbiscuit/CodeSmithAdoNET
测试数据表
--1、Books表 CREATE TABLE [dbo].[Books]( [ID] [varchar](50) NOT NULL, [OrgNo] [varchar](10) NOT NULL, [Name] [varchar](50) NULL, [Num] [int] NULL, [InDate] [datetime] NULL, CONSTRAINT [PK_Books] PRIMARY KEY CLUSTERED ( [ID] ASC, [OrgNo] 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 --2、Books数据 Insert Into [Books] ([ID],[OrgNo],[Name],[Num],[InDate]) Values('1','8002','Mary','4','2017-12-04 09:29:05') Insert Into [Books] ([ID],[OrgNo],[Name],[Num],[InDate]) Values('2','8001','Jim','5','2017-12-03 10:12:11') Insert Into [Books] ([ID],[OrgNo],[Name],[Num],[InDate]) Values('2','8002','Mike','6','2017-12-05 00:00:00') Insert Into [Books] ([ID],[OrgNo],[Name],[Num],[InDate]) Values('2','8003','Jum','7','2017-12-05 00:00:00') Insert Into [Books] ([ID],[OrgNo],[Name],[Num],[InDate]) Values('2','8004','Yury','8','2017-12-09 00:00:00') Insert Into [Books] ([ID],[OrgNo],[Name],[Num],[InDate]) Values('2','8005','Iori','9','2017-12-12 00:00:00') GO --3、Organization表 CREATE TABLE [dbo].[Organization]( [OrgNo] [varchar](5) NOT NULL, [LName] [nvarchar](100) NOT NULL CONSTRAINT [PK_Organization] PRIMARY KEY CLUSTERED ( [OrgNo] 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 --4、Organization数据 Insert Into [Organizations] ([OrgNo],[LName]) Values('8001','组织1') Insert Into [Organizations] ([OrgNo],[LName]) Values('8002','组织2') Insert Into [Organizations] ([OrgNo],[LName]) Values('8003','组织3') Insert Into [Organizations] ([OrgNo],[LName]) Values('8004','组织4') Insert Into [Organizations] ([OrgNo],[LName]) Values('8005','组织5') GO --5、Books视图 CREATE View v_Books as select a.* ,b.LName FROM Books a left JOIN Organization b ON a.OrgNo=b.OrgNo GO
测试程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; namespace Test { class Program { static void Main(string[] args) { GXEIS.BLL.Books BooksBLL = new GXEIS.BLL.Books(); try { /*是否存在记录*/ //bool bIsExists = BooksBLL.Exists("1", "8001"); //Console.WriteLine(bIsExists); /*增加一条记录*/ //GXEIS.Model.Books BooksModel = new GXEIS.Model.Books() //{ // ID = "1", // OrgNo = "8002", // Name = "Mary", // Num = 4, // InDate = DateTime.Now //}; //bool bIsExists = BooksBLL.Add(BooksModel); //Console.WriteLine(bIsExists); /*删除记录*/ //bool bIsExists = BooksBLL.Delete("1", "8001"); //Console.WriteLine(bIsExists); /*更新一条记录*/ //GXEIS.Model.Books BooksModel = new GXEIS.Model.Books() //{ // ID = "2", // OrgNo = "8001", // Name = "Jim", // Num = 5, // InDate = DateTime.Now.AddDays(-1) //}; //bool bIsExists = BooksBLL.Update(BooksModel); //Console.WriteLine(bIsExists); /*得到一个对象实体*/ //GXEIS.Model.Books BooksModel = BooksBLL.GetModel("2", "8001"); //Console.WriteLine(BooksModel.Name); /*得到一个DataSet*/ //DataTable dtResult = BooksBLL.GetList("").Tables[0]; //Console.WriteLine(dtResult.Rows.Count.ToString()); /*获取分页数据,不含时间字段*/ //int iCount = 0; //GXEIS.Model.Books BooksModel = new GXEIS.Model.Books() //{ // ID = "2" //}; //DataTable dtResult = BooksBLL.GetPageList(BooksModel, "ID", 3, 2, out iCount); //Console.WriteLine(dtResult.Rows.Count.ToString()); /*获取分页数据,包含时间字段*/ //int iCount = 0; //GXEIS.Model.Books BooksModel = new GXEIS.Model.Books() //{ // ID = "2" //}; //string sSDate = "2017-12-03"; //string sEDate = "2017-12-05"; //DataTable dtResult = BooksBLL.GetPageList(BooksModel, sSDate, sEDate, "ID", 3, 1, out iCount); //Console.WriteLine(dtResult.Rows.Count.ToString()); /*获取分页视图数据,不含时间字段*/ //int iCount = 0; //GXEIS.Model.Books BooksModel = new GXEIS.Model.Books() //{ // ID = "2" //}; //DataTable dtResult = BooksBLL.GetPageListByView(BooksModel, "ID", 3, 2, out iCount); //Console.WriteLine(dtResult.Rows.Count.ToString()); /*获取分页视图数据,包含时间字段*/ //int iCount = 0; //GXEIS.Model.Books BooksModel = new GXEIS.Model.Books() //{ // ID = "2" //}; //string sSDate = "2017-12-03"; //string sEDate = "2017-12-05"; //DataTable dtResult = BooksBLL.GetPageListByView(BooksModel, sSDate, sEDate, "ID", 3, 1, out iCount); //Console.WriteLine(dtResult.Rows.Count.ToString()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.ReadKey(); } } }