当前位置: 首页 > news >正文

C#如何做简易网站/网站友链交换平台

C#如何做简易网站,网站友链交换平台,宣传型网站的实现技术手段,建设网站需要什么证件Warensoft Unity3D通信库使用向导5-对SQL SERVER进行增、删、改、查操作 (作者:warensoft,有问题请联系warensoft163.com) 在前一节《warensoft unity3d通信库使用向导4-SQL Server访问组件说明》中已经对数据访问组件的结构做了简单的介绍,本节将说明如何具体利用…

Warensoft Unity3D通信库使用向导5-对SQL SERVER进行增、删、改、查操作

(作者:warensoft,有问题请联系warensoft@163.com)

在前一节《warensoft unity3d通信库使用向导4-SQL Server访问组件说明》中已经对数据访问组件的结构做了简单的介绍,本节将说明如何具体利用这些组件对SQL SERVER进行操作。

无论进行哪种操作,首先都要建立DataContext类的实例,建立方法如下所示:

 

//通信管理器类private UnityCommunicationManager cm;//数据库连接管理器private DataContext context;public void Start(){//创建通信管理器的实例

cm = UnityCommunicationManager.CreateInstance();//创建数据库连接管理器的实例,CreateDataContext中的参数就是数据服务的地址this.context = this.cm.CreateDataContext("http://localhost:20869/Web/Default.aspx");//注册Error事件,该事件会在数据库连接发生异常时触发this.context.Error+=new EventHandler<ErrorEventArgs>(this.OnContextError);//当指定的数据库中的数据表结构加载完毕时触发,该操作只加载表结构,不加载表中的数据this.context.SchemaLoadCompleted += new EventHandler(context_SchemaLoadCompleted);}

 

 

 

代码说明:

  • DataContext类实例的构造过程要放在Start()方法中
  • DataContext类不能直接利用其构造函数构造,应用使用UnityCommunicationManager.CreateDataContext(string)方法构造,其参数就是数据服务的地址
  • DataContext中的Error事件,会在数据连接出现异常时触发
  • DataContext类在构造的时候,会向远程服务器发出请求以获取所要连接数据库的表结构。在表结构获取完毕之前,是不能对其进行任何数据操作的。DataContext中提供了一个SchemaLoadCompleted事件,该事件会在表结构加载完毕后触发。用户可以在该事件的回调该当中处理逻辑,该事件是在某一次Update()中被触发的,因此对于Unity3D来说,是线程安全的。另外,如果用户希望自己在C#脚本的Update()方法中控制DataContext以实现数据操作,可以使用DataContext类中的SchemaLoaded属性来判断表结构是否加载完毕。

 

 

加载数据

下面的代码描述了如何从服务器上获取tb_Students表中获取全部数据,并将其分页显示在Unity3D的GameView中,其完全内容如下所示:

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using UnityEngine;using Warensoft.Unity.Communication.Client.DataClient;using Warensoft.Unity.Communication.Client;public class Paging : MonoBehaviour{UnityCommunicationManager cm;DataContext context;public void Start(){cm = UnityCommunicationManager.CreateInstance();this.context = this.cm.CreateDataContext("http://localhost/dataservice/Default.aspx");this.context.Error += new EventHandler<ErrorEventArgs>(this.OnContextError);this.context.SchemaLoadCompleted += new EventHandler(context_SchemaLoadCompleted);}void context_SchemaLoadCompleted(object sender, EventArgs e){//加载表中所有记录this.context.Tables["tb_Students"].LoadAsync(() =>{//计算分页数量this.pageCount = this.context.Tables["tb_Students"].Count / this.pageCapacity;if (this.context.Tables["tb_Students"].Count%this.pageCapacity!=0){this.pageCount++;}//通知OnGUI可以进行显示this.drawTable = true;});}public void OnContextError(object sender, ErrorEventArgs e){print(e.Message);}private bool drawTable = false;private int pageNumber = 0;private int pageCapacity = 5;private int pageCount = 0;public void OnGUI(){if (this.drawTable){//计算该分页中的数据var data = this.context.Tables["tb_Students"].Entities.Skip(this.pageNumber * this.pageCapacity).Take(this.pageCapacity );//绘制窗口

GUI.Window(0, new Rect(0, 0, 400, 300), (id) =>{int row = 30;foreach (var entity in data){//显示第一列,StudentName

GUI.Label(new Rect(10, row, 100, 20), entity["StudentName"].ToString());//显示第二列,Age

GUI.TextField(new Rect(100, row, 100, 20), entity["Age"].ToString());//显示第三列,Gender

GUI.Label(new Rect(230, row, 60, 20), entity["Gender"].ToString() == "True" ? "Male" : "Female");row += 50;}//上翻页按钮if (GUI.Button(new Rect(80, 270, 80, 20), "Previous")){if (this.pageNumber > 0){this.pageNumber--;}}//下翻页按钮if (GUI.Button(new Rect(180, 270, 80, 20), "Next")){if (this.pageNumber < this.pageCount - 1){this.pageNumber++;}}}, "tb_Students");}}}

 

 

 

GameView中的效果如下图所示:

 

通过查询条件加载数据

上面的代码是一次性将单表数据加载到客户的,这种处理方式可以提高分页时的速度,保证界面不出现闪烁。但是如果单表数据量很大的话,这种方案就不是一个好选择了,因此,可以采用带查询条件的加载方式,关于如何利用DataQuery类构建条件查询,请参考《warensoft unity3d通信库使用向导4-SQL Server访问组件说明》,条件查询的完整代码如下所示:

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using UnityEngine;using Warensoft.Unity.Communication.Client.DataClient;using Warensoft.Unity.Communication.Client;public class Paging : MonoBehaviour{UnityCommunicationManager cm;DataContext context;public void Start(){cm = UnityCommunicationManager.CreateInstance();this.context = this.cm.CreateDataContext("http://localhost:20869/Web/Default.aspx");this.context.Error += new EventHandler<ErrorEventArgs>(this.OnContextError);this.context.SchemaLoadCompleted += new EventHandler(context_SchemaLoadCompleted);}void context_SchemaLoadCompleted(object sender, EventArgs e){//设置查询条件var query = new DataQuery();//加载表中所有记录this.context.Tables["tb_Students"].LoadAsync(() =>{//计算分页数量this.pageCount = this.context.Tables["tb_Students"].Count / this.pageCapacity;if (this.context.Tables["tb_Students"].Count%this.pageCapacity!=0){this.pageCount++;}//通知OnGUI可以进行显示this.drawTable = true;},query.GreaterThanOrEqualsTo("Age",10).Take(50));}public void OnContextError(object sender, ErrorEventArgs e){print(e.Message);}private bool drawTable = false;private int pageNumber = 0;private int pageCapacity = 5;private int pageCount = 0;public void OnGUI(){if (this.drawTable){//计算该分页中的数据var data = this.context.Tables["tb_Students"].Entities.Skip(this.pageNumber * this.pageCapacity).Take(this.pageCapacity );//绘制窗口

GUI.Window(0, new Rect(0, 0, 400, 300), (id) =>{int row = 30;foreach (var entity in data){//显示第一列,StudentName

GUI.Label(new Rect(10, row, 100, 20), entity["StudentName"].ToString());//显示第二列,Age

GUI.TextField(new Rect(100, row, 100, 20), entity["Age"].ToString());//显示第三列,Gender

GUI.Label(new Rect(230, row, 60, 20), entity["Gender"].ToString() == "True" ? "Male" : "Female");row += 50;}//上翻页按钮if (GUI.Button(new Rect(80, 270, 80, 20), "Previous")){if (this.pageNumber > 0){this.pageNumber--;}}//下翻页按钮if (GUI.Button(new Rect(180, 270, 80, 20), "Next")){if (this.pageNumber < this.pageCount - 1){this.pageNumber++;}}}, "tb_Students");}}}

 

 

 

 

插入数据

数据插入操作的过程,和利用DataSet+DataAdapter插入数据的方式很类似,其基本过程如下所示:

  1. 希望往哪个表中插入数据,就先利用该表创建一个新的实体,该实体不包含数据,但是和创建该实体所在的表具备相同的构架,代码如下所示:

    var newStudent=this.context.Tables["tb_Students"].NewEntity();

  2. 给新建立的实体中的属性赋值,代码如下所示:

    newStudent["ID"]=Guid.NewGuid();

    newStudent["Age"]=10;

    newStudent["Gender"]=false;

  3. 将新建立的实体加入实体集,代码如下所示:

    this.context.Tables["tb_Students"].Add(newStudent);

  4. 保存修改,提交到服务器,代码如下所示:

    this.context.SaveChanges();

完整代码如下所示:

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using UnityEngine;using Warensoft.Unity.Communication.Client.DataClient;using Warensoft.Unity.Communication.Client;public class Insert : MonoBehaviour {//通信管理器类private UnityCommunicationManager cm;//数据库连接管理器private DataContext context;public void Start(){//创建通信管理器的实例

cm = UnityCommunicationManager.CreateInstance();//创建数据库连接管理器的实例,CreateDataContext中的参数就是数据服务的地址this.context = this.cm.CreateDataContext("http://localhost:20869/Web/Default.aspx");//注册Error事件,该事件会在数据库连接发生异常时触发this.context.Error+=new EventHandler<ErrorEventArgs>(this.OnContextError);//当指定的数据库中的数据表结构加载完毕时触发,该操作只加载表结构,不加载表中的数据this.context.SchemaLoadCompleted += new EventHandler(context_SchemaLoadCompleted);}public void OnContextError(object sender, ErrorEventArgs e){print(e.Message);}void context_SchemaLoadCompleted(object sender, EventArgs e){this.drawTable = true;}private bool drawTable = false;string age="";string gender="";string name="";void OnGUI(){//如果表结构已经加载完毕,可以绘制以下内容if (this.drawTable){GUI.Window(0, new Rect(0, 0, 300, 300), (id) =>{//第一个文本框用于输入年龄this.age = GUI.TextField(new Rect(30, 30, 80, 20), this.age);//第二个文本框用于输入性别this.gender = GUI.TextField(new Rect(30, 50, 80, 20), this.gender);//第三个文本框用于输入名字this.name = GUI.TextField(new Rect(30, 70, 80, 20), this.name);//点击Insert按钮的Click事件if (GUI.Button(new Rect(30, 200, 100, 20), "Insert")){//创建一个和tb_Students表结构一样的空实体var entity = this.context.Tables["tb_Students"].NewEntity();//给ID字段赋值

entity["ID"] = Guid.NewGuid();//给StudentName字段赋值

entity["StudentName"] = this.name;//给Age字段赋值

entity["Age"] = int.Parse(this.age);//给Gender字段赋值

entity["Gender"] = this.gender == "Male" ? true : false;//将新建立的实体加入到表中this.context.Tables["tb_Students"].Add(entity);//保存添加操作this.context.SaveChanges();}}, "Insert Test");}}}

 

 

 

 

修改和删除数据

修改和删除的步骤基本类似,大体要经过如下几个步骤:

    1. 首先要找到对应的实体,代码如下所示:

      var student=this.context.Tables["tb_Students"].FirstOrDefault(s=>s.StudentName=='user1');

    2. 如果要删除该实体,则调用其Delete方法,代码如下所示:

      if(student!=null)

      {

          student.Delete();

      }

    3. 如果是要修改,则直接修改该实体中对应的属性值,代码如下所示:

      student["Age"]=10;

      student["Gender"]=true;

    4. 保修修改,代码如下所示:

      this.context.SaveChanges();

完整的修改和删除的代码如下所示:

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using UnityEngine;using Warensoft.Unity.Communication.Client.DataClient;using Warensoft.Unity.Communication.Client;public class Paging : MonoBehaviour{UnityCommunicationManager cm;DataContext context;public void Start(){cm = UnityCommunicationManager.CreateInstance();this.context = this.cm.CreateDataContext("http://localhost:20869/Web/Default.aspx");this.context.Error += new EventHandler<ErrorEventArgs>(this.OnContextError);this.context.SchemaLoadCompleted += new EventHandler(context_SchemaLoadCompleted);}void context_SchemaLoadCompleted(object sender, EventArgs e){//设置查询条件var query = new DataQuery();//加载表中所有记录this.context.Tables["tb_Students"].LoadAsync(() =>{//计算分页数量this.pageCount = this.context.Tables["tb_Students"].Count / this.pageCapacity;if (this.context.Tables["tb_Students"].Count%this.pageCapacity!=0){this.pageCount++;}//通知OnGUI可以进行显示this.drawTable = true;},query.GreaterThanOrEqualsTo("Age",10).Take(50));}public void OnContextError(object sender, ErrorEventArgs e){print(e.Message);}private bool drawTable = false;private int pageNumber = 0;private int pageCapacity = 5;private int pageCount = 0;public void OnGUI(){if (this.drawTable){//计算该分页中的数据var data = this.context.Tables["tb_Students"].Entities.Skip(this.pageNumber * this.pageCapacity).Take(this.pageCapacity );//绘制窗口

GUI.Window(0, new Rect(0, 0, 400, 300), (id) =>{int row = 30;foreach (var entity in data){//显示第一列,StudentName

GUI.Label(new Rect(10, row, 100, 20), entity["StudentName"].ToString());//显示第二列,Age,该行代码表示,Age列可以修改

entity["Age"] = int.Parse(GUI.TextField(new Rect(100, row, 100, 20), entity["Age"].ToString()));//显示第三列,Gender

GUI.Label(new Rect(230, row, 60, 20), entity["Gender"].ToString() == "True" ? "Male" : "Female");//删除按钮if (GUI.Button(new Rect(300, row, 100, 20), "Delete")){entity.Delete();}row += 50;}//上翻页按钮if (GUI.Button(new Rect(80, 270, 80, 20), "Previous")){if (this.pageNumber > 0){this.pageNumber--;}}//下翻页按钮if (GUI.Button(new Rect(180, 270, 80, 20), "Next")){if (this.pageNumber < this.pageCount - 1){this.pageNumber++;}}//保存按钮if (GUI.Button(new Rect(280, 270, 80, 20), "Save")){this.context.SaveChanges(RefreshMode.ClientWins);}}, "tb_Students");}}}

 

 

 

 

 

转载于:https://www.cnblogs.com/warensoft/archive/2012/05/01/2478169.html

http://www.jmfq.cn/news/4740751.html

相关文章:

  • 专门做代工产品的网站/百度爱采购推广一个月多少钱
  • 百度推广和网站建设/中国广告网
  • 网站文章模块/活动策划方案详细模板
  • 兼职网站建设 开源/线上销售怎么做推广
  • wordpress生成原生app/seo免费优化公司推荐
  • 广州专业网站建设哪家公司好/今日新闻头条最新消息
  • 做淘客网站需要什么/网站策划方案书
  • 苍溪县城乡建设投资有限公司网站/百度网址大全简单版
  • 网站设计流程的步骤/谷歌排名优化入门教程
  • 购物网站建设思路/seo 知乎
  • 石家庄抖音seo公司/网站推广优化是什么意思
  • 网站常见 8/南京今日新闻头条
  • 建设网站目的/seo关键词大搜
  • 兰溪市建设局网站/百度2022年版本下载
  • 做企业内部网站要多久/全网关键词搜索排行
  • 做网站跑matlab程序/网络营销的方法
  • 快速做网站联系电话/深圳网络营销和推广方案
  • 临清建网站/软文文案案例
  • 福州鼓楼区网站建设/南昌seo排名收费
  • 如何创立自己的网站/地推扫码平台
  • 重庆建站管理系统信息/google网站推广
  • 全国美容网站建设/网络营销推广的概念
  • 做图素材的网站有哪些/怎样做自己的网站
  • 广州做营销型网站建设/网络营销首先要做什么
  • 网站建设公司类型/网站关键词优化方案
  • 怎么做网站解析/镇江网站seo
  • 帮公司做网站运营/关键词优化外包服务
  • 域名购买网站有哪些问题/国际新闻今天
  • 公司创建网站销售/女装标题优化关键词
  • wordpress做个人教学网站/宁波seo外包优化公司