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

10月 23

更新方法

只需要有ID号就可以了

需要更新哪个就给哪个赋值即可

    Student student = new Student();
        student.ID = 2;
        student.Password = "updatePassword";
        student.Update();

执行后结果如下

written by ocean

10月 23

举例的数据表里有

Student

public int ID

public string UserName

public string Password

public int Age

public bool IsBoy

public int SchoolID

public decimal Height

public DateTime AddTime

School

public int ID

public string SchoolName

Student表里面的Height表示身高,用的decimal类型,只是为了演示一下该类型字段而已

SchoolID为学校代码外键,其它意义很明显,就不做介绍了

   代码如下

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();

执行以上代码就可以插入数据了 School表3条 ,Student表2条

执行后结果如下

written by ocean

10月 23

任何ORM框架都会有约定,我的也不例外,目前约定如下

1.实体类名和数据表名相同,视图也可以,视图名也要和实体类名相同

2,主键必须是"ID",并且主键是自增的整数(如果不自增需要指定),这个很重要.

3,数据表的字段名和属性名相同.

4,正确使用.

只要遵循以上的约定,就可以正确的使用我的ORM框架了。

当然了,web.config里面是需要配置一下的
        <!–Access数据库连接字符串–>
        <add key="AccessConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|test.mdb;Persist Security Info=False"/>
        <!–Sql Server数据库连接字符串–>
        <add key="SqlServerConnectionString" value="server=.;database=YsuhyORMTest;uid=sa;pwd=zxcvbnm,./"/>
        <!–是否启用反射缓存,建议设为True–>
        <add key="IsCachePropertyInfo" value="true" />
        <!–数据访问方式 Access 和 SqlServer–>
        <add key="DALMethod" value="Access"/>

都写上注释了,就不用一一解释了

最后生成一下实体类代码
调用方法,如果是网站项目的话,直接调用一下
Ysuhy.Tools.CreateModel.CreateFile();
方法就可以在 /AppCode/Model/下生成所有的实体类代码
如果是WinForm的话,调用一下
Ysuhy.Tools.CreateModel.CreateFile(@"F:\ModeCode");
就可以在F:\ModeCode路径下生成所有实体类代码

只需以上两步,就可以调用我的ORM方法,轻松实现对数据持久的操作了,是不是比起Nhibernate繁琐的配置方便了很多

举例的数据表里有

Student

public int ID

public string UserName

public string Password

public int Age

public bool IsBoy

public int SchoolID

public decimal Height

public DateTime AddTime

School

public int ID

public string SchoolName

Student表里面的Height表示身高,用的decimal类型,只是为了演示一下该类型字段而已

SchoolID为学校代码外键,其它意义很明显,就不做介绍了

written by ocean

10月 23

YsuhyORM 是我继AutoCode(代码生成器)后,开发的第二套数据持久解决方案. 是一套轻量级ORM框架.

开发这个程序的初衷是ORM良好的扩展性,添加字段不需要修改访问层.而在我研究了Nhibernate 后,觉得这个东东太过于庞大了,太过于功能齐全了,对于一般的应用来说,我并没有那么多的需求,不需要为做那么多的功能和兼容性,因此而损失很多的性能.

于是我就又造了一次轮子,在造轮子的过程中,我学到了很多东西,因而乐此不疲,并且打算以后还会造一些适合自己的轮子.在实际应用中,我也会不断完善这个轮子,遇见新的问题我会添加新的功能,我想这就是自己造轮子的最大乐趣吧.

现在说下这个ORM的目前状况吧,目前支持SqlServer,Access两种数据库.只需在Web.Config做简单配置即可.个人已经做过测试,在Sql Server 2005里面操作100000记录的时候查询速度影响很小,而100000数据对于一个小型系统来说应该足够了.添加,删除,修改100条数据的时间和直接操作的时间也相差非常小,我觉得在一般系统里完全可以忽略差距,因而此ORM完全可以胜任.

更多数据我还没有测试,等过几天有时间了我会把测试的结果贴在这里的.目前该ORM框架基于.NET2.0边写,现在有方法11个,我会逐一介绍一下的,基本上就算是一个API吧

YsuhyORM 配置 示例数据库

YsuhyORM之Add方法

YsuhyORM之Update方法

YsuhyORM之Delete方法

YsuhyORM之GetModel方法

YsuhyORM之Exist

YsuhyORM之GetList方法

YsuhyORM之ChangeTableName方法

YsuhyORM之New方法

YsuhyORM之GetSingle方法

YsuhyORM之GetCount方法

YsuhyORM之GetList<T>方法

YsuhyORM之GetId方法

YsuhyORM之GetMaxId方法

方法已经更新到13个

written by ocean

10月 21

var check=function(e){
   e=e||window.event;
   //alert(e.which||e.keyCode);
   if((e.which||e.keyCode)==116){
    if(e.preventDefault){
    e.preventDefault();}
    else{event.keyCode = 0;
     e.returnValue=false;}
   }
}
if(document.addEventListener){
   document.addEventListener("keydown",check,false);
}
else{
   document.attachEvent("onkeydown",check);
}

做个记录

written by ocean

10月 16

以下为模拟登陆保持状态访问其它页面的全过程

访问的是aspx页面,所以要先获取以下viewstate

    CookieContainer cc = new CookieContainer();
    protected void Page_Load(object sender, EventArgs e)
    {

        CookieContainer cookieContainer = new CookieContainer();

        //1,登陆页面,获取到需要准备的条件,也就是ViewState
        string URI = "http://localhost:4886/Login.aspx";
        HttpWebRequest request = WebRequest.Create(URI) as HttpWebRequest;
        request.Method = "POST";
        request.KeepAlive = false;
        // 接收返回的页面
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        System.IO.Stream responseStream = response.GetResponseStream();
        System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
        string srcString = reader.ReadToEnd();
        // 获取页面的 VeiwState               
        string viewStateFlag = "id=\"__VIEWSTATE\" value=\"";
        int i = srcString.IndexOf(viewStateFlag) + viewStateFlag.Length;
        int j = srcString.IndexOf("\"", i);
        string viewState = srcString.Substring(i, j – i);
        // 获取页面的 EventValidation               
        string eventValidationFlag = "id=\"__EVENTVALIDATION\" value=\"";
        i = srcString.IndexOf(eventValidationFlag) + eventValidationFlag.Length;
        j = srcString.IndexOf("\"", i);
        string eventValidation = srcString.Substring(i, j – i);

       
        //2,开始登陆页面
        string Button = "Button1";
        string TextBox1 = "Admin";
        string TextBox2 = "Admin888";
        // 将文本转换成 URL 编码字符串
        viewState = System.Web.HttpUtility.UrlEncode(viewState);
        eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
        Button = System.Web.HttpUtility.UrlEncode(Button);

        // 要提交的字符串数据。
        string formatString =
                 "TextBox1={0}&TextBox2={1}&Button={2}&__VIEWSTATE={3}&__EVENTVALIDATION={4}";
        string postString =
                 string.Format(formatString, TextBox1, TextBox2, Button, viewState, eventValidation);
        // 将提交的字符串数据转换成字节数组
        byte[] postData = Encoding.ASCII.GetBytes(postString);
        // 设置提交的相关参数
        request = WebRequest.Create(URI) as HttpWebRequest;
        request.Method = "POST";
        request.KeepAlive = false;
        request.ContentType = "application/x-www-form-urlencoded";
        request.CookieContainer = cookieContainer;
        request.ContentLength = postData.Length;
        // 提交请求数据
        System.IO.Stream outputStream = request.GetRequestStream();
        outputStream.Write(postData, 0, postData.Length);
        outputStream.Close();
        // 接收返回的页面
        response = request.GetResponse() as HttpWebResponse;
        responseStream = response.GetResponseStream();
        reader = new System.IO.StreamReader(responseStream);
        srcString = reader.ReadToEnd();

       
        //现在可以打开需要登录才能访问的页面了
        URI = "http://localhost:4886/YesLogin.aspx";
        request = WebRequest.Create(URI) as HttpWebRequest;
        request.Method = "GET";
        request.KeepAlive = false;
        request.CookieContainer = cookieContainer;//这个就是登陆的Cookie
        // 接收返回的页面
        response = request.GetResponse() as HttpWebResponse;
        responseStream = response.GetResponseStream();
        reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
        srcString = reader.ReadToEnd();
    }

以下为登陆页面的前后台源码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="CDNDemo.Login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    noLogin
        <asp:TextBox ID="TextBox1" runat="server" Text="Admin"></asp:TextBox><asp:TextBox Text="Admin888" ID="TextBox2"
            runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server"
            Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace CDNDemo
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text.Trim() == "Admin" && TextBox2.Text.Trim() == "Admin888")
            {
                Session["Admin"] = "Admin";
            }
        }
    }
}

以下为需要登录才能访问页面的前后台源码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="YesLogin.aspx.cs" Inherits="CDNDemo.YesLogin" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    fd
    </div>
    </form>
</body>
</html>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace CDNDemo
{
    public partial class YesLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Admin"] == null)
            {
                Response.Redirect("Login.aspx");
                Response.Write("success");
            }
        }
    }
}

written by ocean