11月 04
因为这个框架约定了主键必须为ID,因此为了有时候方便一点,提供了GetId方法用来获取某些数据的ID号例如,我们知道学生的姓名叫小刘,但是我们现在要更新他的密码示例代码

written by ocean \\ tags:

11月 04

该方法基本同GetLIst方法,唯一不同是返回List T 结构数据

共有四个重载方法

各参数同GetList方法

pageIndex页面,这个需要注意一下 如果-1的话会返回全部记录

pageSize,每次获取的记录集记录条数,如果pageIndex=-1时则无效

column,需要获取的列,默认为"*",即全部

where,获取记录集的条件

order,排序,排序字段目前固定为ID,true为正序,false为逆序,默认为false

Count,满足条件的记录总条数,通常用来前台分页


示例代码

written by ocean

11月 04

顾名思义,GetCount就是用来获取记录条数的

该方法有三个重载

public int GetCount(string where)

public int GetCount()

public int GetCount(string majorTableName, string minorTableName, string relationString,
            string majorWhere, string minorWhere)

默认无参数,返回当前表的所有记录条数

where参数为具体条件,例如 age>17, SchoolID=2等

第三个方法的具体参会会在GetRelationList方法中做具体介绍

示例代码


written by ocean

11月 04
    该方法共有5个重载 
public bool GetSingle(string columnName,out int result)
 
public bool GetSingle(string columnName, out string result)
 
public bool GetSingle(string columnName, out decimal result)
 
public bool GetSingle(string columnName, out bool result)
 
public bool GetSingle(string columnName, out DateTime result)
 
使用时注意 首先需要设置ID号主键才可以
 
示例代码     
 
        Student s = new Student();
        DataTable dt = s.GetList(-1, 0);       
        for (int index = 0; index < dt.Rows.Count; index++)
        {
            int Sid = Convert.ToInt32(dt.Rows[index]["ID"]);
 
            s.ID = Sid;
            int Age;
            if (s.GetSingle(EnumStudent.Age.ToString(), out Age))
            { Response.Write(string.Format("{0}的age是{1}<br>", dt.Rows[index]["username"].ToString(), Age)); }
            else
            { Response.Write(string.Format("{0}的age获取失败<br>", dt.Rows[index]["username"].ToString())); }
 
            s.New();
            s.ID = Sid;
            DateTime AddTime;
            if (s.GetSingle(EnumStudent.AddTime.ToString(), out AddTime))
            { Response.Write(string.Format("{0}的AddTime是{1}<br>", dt.Rows[index]["username"].ToString(), AddTime)); }
            else
            { Response.Write(string.Format("{0}的AddTime获取失败<br>", dt.Rows[index]["username"].ToString())); }
 
            s.New();
            s.ID = Sid;
            decimal Height;
            if (s.GetSingle(EnumStudent.Height.ToString(), out Height))
            { Response.Write(string.Format("{0}的Height是{1}<br>", dt.Rows[index]["username"].ToString(), Height)); }
            else
            { Response.Write(string.Format("{0}的Height获取失败<br>", dt.Rows[index]["username"].ToString())); }
        }
  
  
  
数据结构

执行结果

written by ocean

11月 02

如果您仔细看过之前的代码,一定会发现我有时候会用一个

student.New()的方法

这个方法主要用来在给Model赋值的时候用,以便于在最后生成SQL的时候只更新需要更新的字段内容,而不是更新全部

其实也就相当与一个初始化的函数,避免连续更新或者添加的时候相互之间有影响

示例代码

    School school = new School(); 
        school.SchoolName = "复旦大学";
        school.Add();
 
        school.New();
        school.SchoolName = "南京大学";
        school.Add();
 
        school.New();
        school.SchoolName = "西安交通大学";
        school.Add();
 
 
        Student student = new Student();
 
        student.UserName = "小红";
        student.SchoolID = 1;
        student.Password = "xiaohong";
        student.IsBoy = false;
        student.Height = (decimal)1.623;
        student.Age = 19;
        student.AddTime = DateTime.Now;
        student.Add();
 
        student.New();
        student.UserName = "小刚";
        student.AddTime = DateTime.Now;
        student.Age = 17;
        student.IsBoy = true;
        student.SchoolID = 2;
        student.Add();

written by ocean

11月 02

有时候会遇见相同结构的表,但是表名称不同

比方说在做多语言站点的时候,可以把中文和英文的对应内容存入两个表中,两个表的结构相同,根据cookie获取相应的表里的的内容

这个时候就可以用到ChangeTableName方法,其实主要是用来替换数据表运用GetList的

示例如下

written by ocean

10月 26

GetList,顾名思义,获取分页列表数据

在YsuhyORM里面是返回DataTable格式的数据集,为了看见测试效果,我在加入几条记录,代码如下

        Student student = new Student();

        student.New();
        student.AddTime = System.DateTime.Now;
        student.Age = 16;
        student.Height = (decimal)169.15;
        student.IsBoy = false;
        student.Password = "gggg";
        student.SchoolID = 3;
        student.UserName = "小华";
        student.Add();

        student.New();
        student.AddTime = System.DateTime.Now;
        student.Age = 16;
        student.Height = (decimal)169.15;
        student.IsBoy = false;
        student.Password = "gggg";
        student.SchoolID = 3;
        student.UserName = "小强";
        student.Add();

        student.New();
        student.AddTime = System.DateTime.Now;
        student.Age = 16;
        student.Height = (decimal)169.15;
        student.IsBoy = false;
        student.Password = "gggg";
        student.SchoolID = 3;
        student.UserName = "小刘";
        student.Add();

        student.New();
        student.AddTime = System.DateTime.Now;
        student.Age = 16;
        student.Height = (decimal)169.15;
        student.IsBoy = false;
        student.Password = "gggg";
        student.SchoolID = 3;
        student.UserName = "小李";
        student.Add();

        student.New();
        student.AddTime = System.DateTime.Now;
        student.Age = 16;
        student.Height = (decimal)169.15;
        student.IsBoy = false;
        student.Password = "gggg";
        student.SchoolID = 3;
        student.UserName = "小张";
        student.Add();

        student.New();
        student.AddTime = System.DateTime.Now;
        student.Age = 16;
        student.Height = (decimal)169.15;
        student.IsBoy = false;
        student.Password = "gggg";
        student.SchoolID = 3;
        student.UserName = "小王";
        student.Add();

        student.New();
        student.AddTime = System.DateTime.Now;
        student.Age = 16;
        student.Height = (decimal)169.15;
        student.IsBoy = false;
        student.Password = "gggg";
        student.SchoolID = 3;
        student.UserName = "小云";
        student.Add();

执行后结果如下:


GetList方法目前有四个重载

先解释一下各参数用法

pageIndex页面,这个需要注意一下 如果-1的话会返回全部记录

pageSize,每次获取的记录集记录条数,如果pageIndex=-1时则无效

column,需要获取的列,默认为"*",即全部

where,获取记录集的条件

order,排序,排序字段目前固定为ID,true为正序,false为逆序,默认为false

Count,满足条件的记录总条数,通常用来前台分页

示例调用代码1

     Student student = new Student();
        DataTable dt = student.GetList(-1,0);
        GridView1.DataSource = dt;
        GridView1.DataBind();

结果如下

示例调用代码2

        Student student = new Student();
        int count = 0;
        DataTable dt = student.GetList(2,2,out count);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        Label1.Text = string.Format("共有记录{0}条", count);

结果如下

示例调用代码3

        Student student = new Student();
        int count = 0;
        DataTable dt = student.GetList(2, 2, "ID,UserName", "Age=16", true,out count);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        Label1.Text = string.Format("共有记录{0}条", count);

执行结果


written by ocean

10月 23

判断存在,也可以用来判读登陆         ,

给哪几个值赋值即判断哪几个值的存在

Student student = new Student();

        student.UserName = "小红";
        bool exist=student.Exist();

        student.New();
        student.UserName = "小刚";
        student.Password = "updatePassword";
        bool exist2 = student.Exist();

        student.New();
        student.SchoolID = 3;
        bool exist3 = student.Exist();

执行结果

written by ocean

10月 23

顾名思义,GetModel就是获取该条记录的实体方法,

只需要有ID号即可

代码:

        Student student = new Student();
        student.ID = 1;
        student.GetModel();

执行结果

written by ocean

10月 23

如果要删除某条数据,可以调用如下代码

只要有ID号就可以了

Student student = new Student();
student.ID = 2;
student.Delete();

执行结果就不演示了

written by ocean