Functional Demos

新增数据演示

日志操作


一、基本说明:

要向数据库里插入一条或者多条记录,在Winson.Framework3.0里此操作已变得非常轻松!

在新增操作中,未赋值的字段将会自动以数据库里的默认值代替

二、操作步骤:

注:以下演示均以WinsonFrameDB数据库的 Test 表为例:

1、新增单个实体到数据库:


操作步骤如下:

a.引用以下命名空间:
using Winson.EntityOP;
b.实例化一个实体操作类,此实体操作类已包括了实体类和所有相关的操作方法:
Test test = new Test();
c.为实体赋值,其中 Entity 为实体类属性,所有实体操作类里都可通过此属性直接调用其实体里所有成员属性:

test.Entity.Age = 20;
test.Entity.Name = "Winson";
test.Entity.Datetime = System.DateTime.Now;
d.调用新增的方法,向数据库进行新增操作,此方法返回影响的记录行数:
test.Add();
e.完整的代码如下:

Test test = new Test();
test.Entity.Age = 20;
test.Entity.Name = "Winson";
test.Entity.Datetime = System.DateTime.Now;
test.Entity.Context = "Test context";
if (test.Add() > 0 ) //判断是否添加成功
    Response.Write("success");
else
    Response.Write("fail");

2、批量新增实体到数据库:


批量新增步骤 a-c 与单实体操作一样,从 d 步骤开始要创建一个新实体,同时添加到集合里,以下步骤从 d 开始:

d.需将以上实体添加到集合里,然后再创建另一个新实体,为新实体赋值,不断重复此步骤即可添加多个实体了:

test.AppendEntity(); //将第一个实体追加到实体集合里
test.CreateEntity(); //创建另一个新实体
//为新实体赋值
test.Entity.Age = 30;
test.Entity.Name = "第二个实体";
test.Entity.Datetime = System.DateTime.Now;
test.Entity.Context = "Test context 02";
test.AppendEntity(); //将新实体再追加到实体集合里
e.调用批量新增的方法,向数据库进行新增操作,此方法返回一个布尔值,成功为True,否则为False:
test.AddBatch();
f.完整的代码如下:

Test test = new Test();
test.Entity.Age = 20;
test.Entity.Name = "Winson";
test.Entity.Datetime = System.DateTime.Now;
test.Entity.Context = "Test context";
test.AppendEntity(); //将第一个实体追加到实体集合里
test.CreateEntity(); //创建另一个新实体
//为新实体赋值
test.Entity.Age = 30;
test.Entity.Name = "第二个实体";
test.Entity.Datetime = System.DateTime.Now;
test.Entity.Context = "Test context 02";
test.AppendEntity(); //将新实体再追加到实体集合里
if (test.AddBatch()) //判断是否添加成功
    Response.Write("success");
else
    Response.Write("fail");

3、对 image 数据类型的操作:


在3.0版里已可支持所有数据类型,包括image等byte[]类型,在Test表里有一个image类型的字段,如要向此字段添加数据,只需直接为其赋值即可:

由于image类型是流文件类型,因此需要使用byte[]类型的数据进行操作,以下步骤可以将以下文件转成byte[]数据:

//引用命名空间
using Winson.Framework.Utility;

//获取图片文件(或者其他文件也可以)
byte[] b = Utility.ReadFile(@"d:\01.jpg");

Test test = new Test();
test.Entity.Pic = b;//直接将byte[]数据赋值到相关的字段即可
test.Add();
关于将数据库里的图片文件读出,可以使用 Utility.writeFile 方法实现,此方法已在查询数据栏目里有所介绍了,在此不再详述。
三、效果演示:

1、新增单个实体到数据库:


以下各项为空时则自动使用数据库的默认值进行新增
姓名:
年龄:
是否:
内容:
要添加到数据库的图片绝对路径(如 D:\test.jpg):

       

private void Add()
{
    test = new Test();
    test.Entity.Name = String.IsNullOrEmpty(Request["Name"]) ? "" : Request["Name"];
    test.Entity.Age = String.IsNullOrEmpty(Request["Age"]) ? 0 : Convert.ToInt32(Request["Age"]);
    test.Entity.Context = String.IsNullOrEmpty(Request["Context"]) ? "" : Request["Context"];

    if (!String.IsNullOrEmpty(Request["Pic"]))
    {
        byte[] pic = Utility.ReadFile(@"" + Request["Pic"]);
        test.Entity.Pic = pic;
    }
    if (test.Add() > 0)
        ReturnResult("1");
    else
        ReturnResult("0");
}

2、批量新增实体到数据库:


以下各项为空时则自动使用数据库的默认值进行新增
姓名:
年龄:
内容:
要添加到数据库的图片绝对路径(如 D:\test.jpg):
要增加实体的个数:

       

private void AddBatch()
{
	test = new Test();
	int count = String.IsNullOrEmpty(Request["Count"]) ? 1 : Convert.ToInt32(Request["Count"]);

	for (int i = 0; i < count; i++)
	{
		test.Entity.Name = String.IsNullOrEmpty(Request["Name"]) ? "" : Request["Name"];
		test.Entity.Age = String.IsNullOrEmpty(Request["Age"]) ? 0 : Convert.ToInt32(Request["Age"]);
		test.Entity.Context = String.IsNullOrEmpty(Request["Context"]) ? "" : Request["Context"];
		if (!String.IsNullOrEmpty(Request["Pic"]))
		{
			byte[] pic = Utility.ReadFile(@"" + Request["Pic"]);
			test.Entity.Pic = pic;
		}
		//上面的第一次不用创建,从第二个实体开始就需要创建新实体了
		test.AppendEntity();
		test.CreateEntity();
	}
	if (test.AddBatch())
		ReturnResult("1");
	else
		ReturnResult("0");
}