问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

使用datagridview 进行数据的增删改查.

发布网友 发布时间:2022-04-07 18:51

我来回答

2个回答

懂视网 时间:2022-04-07 23:12

UnityEngine; using System.Collections; using System; using System.IO; using Sqo; public class MyDBFactory { public static string siaoqodbPath; private static Siaqodb instance; public static Siaqodb GetInstance() { if (instance == null) { //if ANDROID: //siaoqodbPath=Application.persistentDataPath; //if Windows or MAC siaoqodbPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + @"mydatabase"; Debug.Log(Environment.CurrentDirectory); //if iOS (iPhone /iPad) //siaoqodbPath =Application.dataPath; if (!Directory.Exists(siaoqodbPath)) { Directory.CreateDirectory(siaoqodbPath); } instance = new Siaqodb(siaoqodbPath); } return instance; } public static void CloseDatabase() { if (instance != null) { instance.Close(); instance = null; } } }

 

数据结构类,相当于数据库表结构类似

using UnityEngine;
using System.Collections;
public class Persss {
 public string Name { get; set; }

 public string Msg { get; set; }


 public int OID { get; set; }
}

数据操作的相对Persss的一个封装类,使数据操作更清晰

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using Sqo;

public class MyPManager {


 public static List<Persss> GetRecentPlayers(int count)
 {
 Siaqodb database = MyDBFactory.GetInstance();

 IEnumerable<Persss> query = (from Persss player in database
     orderby player.OID descending
     select player).Take(count);

 return query.ToList<Persss>();
 }
 public static Persss LoadPlayer(string name)
 {

 Siaqodb database = MyDBFactory.GetInstance();

 Persss p = (from Persss player in database
   where player.Name == name
   select player).FirstOrDefault<Persss>();

 if (p == null)//not exists so create and save
 {
  p = new Persss();
  p.Name = name;
  p.Msg = "未找到";

  
 }
 return p;

 }
 public static int TotalPlayers()
 {
 Siaqodb database = MyDBFactory.GetInstance();

 return database.Count<Persss>();
 }
 public static void SavePlayer(Persss p)
 {
 Siaqodb database = MyDBFactory.GetInstance();
 database.StoreObject(p);
 }
}

 

主要的类

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using Sqo;
using UnityEngine.UI;
using System.Linq;
public class mySiaqodbDemo : MonoBehaviour {
 Siaqodb database;


 public InputField inset_name;
 public InputField inset_msg;

 public InputField read_name;
 public InputField read_msg;
 public InputField read_Oid;

 public Text show_text;

 public InputField up_name;
 public InputField up_msg;
 public InputField up_Oid;


 public InputField del_input;

 // Use this for initialization
 void Start () {
  database = MyDBFactory.GetInstance();
 
 List<Persss> recentPlayers = MyPManager.GetRecentPlayers(MyPManager.TotalPlayers());

 Debug.Log("Perss count is " + MyPManager.TotalPlayers() );
 foreach (Persss p3 in recentPlayers)
 {
  Debug.Log("OID: " +p3.OID.ToString() +" Name:" + p3.Name + " Message:" + p3.Msg +"--end--");
  if(p3.OID<410)
  {
  //database.Delete(p3);
  }
 }

 }

 public void BtnInSertData()
 {
 Persss p = new Persss();
 string n = inset_name.text.Trim();
 string ms = inset_msg.text.Trim();
 if( n.Length >1 && ms.Length >1 )
 {
 p.Name = inset_name.text;
 p.Msg = inset_msg.text;
 MyPManager.SavePlayer(p);
 }
 BtnReadAllData();
 }

 public void BtnReadDataByOID()
 {
 int ood = Convert.ToInt32(read_Oid.text);
 Debug.Log(ood);
 Persss dss = (from Persss p in database where (p.OID == ood) select p).FirstOrDefault();
 if (dss != null)
 {
  //Persss dssp = dss as Persss;
  read_name.text = dss.Name;
  read_msg.text = dss.Msg;
  read_Oid.text = dss.OID.ToString();

  up_Oid.text = dss.OID.ToString();
  up_name.text = dss.Name;
  up_msg.text = dss.Msg;

 }
 else 
 {
  read_Oid.text = "空值";
 }
 
 }


 public void BtnReadAllData()
 { 
  List<Persss> recentPlayers = MyPManager.GetRecentPlayers(MyPManager.TotalPlayers());

 Debug.Log("Perss count is " + MyPManager.TotalPlayers() );
 show_text.text = "";
 foreach (Persss p3 in recentPlayers)
 {
  Debug.Log("OID: " +p3.OID.ToString() +" Name:" + p3.Name + " Message:" + p3.Msg +"--end--");
  Debug.Log("ReadAllDate");
  show_text.text += "OID: " + p3.OID.ToString() + " Name:" + p3.Name + " Message:" + p3.Msg + "--end--";
 }
  
 }


 public void BtnUpdateDataByOID()
 {
 Persss u = (from Persss p in database where (p.OID == Convert.ToInt32(up_Oid.text)) select p).FirstOrDefault();
 Debug.Log( up_Oid.text);
 if (u != null)
 {
  u.Name = up_name.text;
  u.Msg = up_msg.text;
  // u.OID =Convert.ToInt32( up_Oid.text );
  Debug.Log("UpdataByOID");
  database.StoreObject(u);
 }

 BtnReadAllData();
 }

 public void BtnDeleteDataByName()
 {
 Persss dss = (from Persss p in database where (p.Name == del_input.text ) select p).FirstOrDefault();

 if (dss != null)
 {
  database.Delete(dss);
 }
 BtnReadAllData();
 }

 public void BtnDeleteDataByMsg()
 {
 Persss dss = (from Persss p in database where (p.Msg == del_input.text) select p).FirstOrDefault();

 if (dss != null)
 {
  database.Delete(dss);
 }
 BtnReadAllData();
 }

 public void BtnDeleteDataByOID()
 {
 Persss dss = (from Persss p in database where (p.OID == Convert.ToInt32 (del_input.text)) select p).FirstOrDefault();

 if (dss != null)
 {
  database.Delete(dss);
 }
 BtnReadAllData();
 }

 void OnApplicationQuit()
 {
 database = null;
 }
 
}

 

Unity3D主界面

技术分享

 

插入数据测试

技术分享

 

技术分享

其它功能类似可用

Unity3D使用Siaqodb基本增删改查Demo

标签:

热心网友 时间:2022-04-07 20:20

这样吧,我发一个GridView的增删改查的例子给你:
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OASys.BLL;
using OASys.Models;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{
public UserInfoManager info = new UserInfoManager();

/// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadInfo();
}
}

/// <summary>
/// 加载用户信息的方法
/// </summary>
private void LoadInfo()
{
List<UserInfo> users =new List<UserInfo>();
foreach (UserInfo user in info.GetAllUserInfo())
{
if (user.Gender == 1)
{
user.DepartId.sex = "男";
}
else
{
user.DepartId.sex = "女";
}
users.Add(user);
}
gdvUserInfo.DataSource =users;
gdvUserInfo.DataBind();
}

/// <summary>
/// 分页事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gdvUserInfo.PageIndex = e.NewPageIndex;
LoadInfo();
}

/// <summary>
/// 删除信息事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id =gdvUserInfo.DataKeys[e.RowIndex].Value.ToString();
if (info.DeleteUserInfoById(id) > 0)
{
this.lblMessage.Text = "删除成功!";
LoadInfo();
}
else
{
this.lblMessage.Text = "删除失败!";
LoadInfo();
}
}

/// <summary>
/// 编辑事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowEditing(object sender, GridViewEditEventArgs e)
{
gdvUserInfo.EditIndex = e.NewEditIndex;
LoadInfo();
}

/// <summary>
/// 取消编辑事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gdvUserInfo.EditIndex = -1;
LoadInfo();
}

/// <summary>
/// 更新事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string userId = gdvUserInfo.DataKeys[e.RowIndex].Value.ToString();
TextBox txtUserId = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtUserId");
TextBox txtUserName = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtUserName");
TextBox txtPassWord = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtPassWord");
if (txtUserName != null && txtPassWord != null)
{
try
{
UserInfo user = info.GetUserInfoById(userId);
user.UserName = txtUserName.Text.ToString();
user.Password = txtPassWord.Text.ToString();
if (info.ModifyUserInfo(user) > 0)
{
this.lblMessage.Text = "修改成功!";
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
else
{
this.lblMessage.Text = "修改失败!";
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
}
catch (Exception)
{

this.lblMessage.Text = "修改失败!";
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
}
}

/// <summary>
///
/// 光棒事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < gdvUserInfo.Rows.Count; i++)
{
if (gdvUserInfo.Rows[i].RowType == DataControlRowType.DataRow)
{
gdvUserInfo.Rows[i].Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#9cf'");
gdvUserInfo.Rows[i].Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}
}追问能不能把项目发我邮箱?

追答恩

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
八月中国最凉快的地方 八月份哪里最凉快,去哪旅游好?美丽的地方 乱字同韵字是什么意思 华硕笔记本电脑触摸板怎么开笔记本电脑触摸板怎么开启和关闭_百度知 ... 陕西职务侵占案立案准则 结婚后我的恋情维系了十年,怎么做到的? 玉米仁子饭产自哪里 中国期货交易所的交易品种有哪些? 历史要怎么读,有啥诀窍 高中历史诀窍 系统提示“丢失uplay_r1_loader64.dll”该怎么解决? 3DM上下载的孤岛惊魂4 中文硬盘版 不能进游戏 提示没有安装 uplay 看门狗弹出Unable to start Uplay Is it installed 刺客信条起源无法检测到Uply怎么弄 为什么在Uplay上玩刺客信条2提示检查你的游戏是否正确安装? 家里有个老的高压锅,手柄上有gainrai的英文字母,请问是什么牌子的 多克隆抗体的纯化方法都有哪些?急需高手讲解 苹果8小点点怎么设置 刺客信条奥德赛cpy版本安装了,打了破解补丁,还是跳出uplay未正确安装。求解决!!!!! 有没有微博运营团队构架图? 正在做抗体纯化,不知哪家纯化介质比较好 保险公司运营组织结构 可以用火腿代替肉吗 炸酱面不想用五花肉,可以用别的火腿肠之类的代替吗? 奶油炖菜里的鸡块可以用火腿代替吗? 哥本哈根减肥没有低脂火腿用美好火腿肠替可以吗 蒸火腿的时候没有料酒可以用什么代替 哥本哈根食谱买不到低脂火腿可以用无淀粉的火腿肠代替吗? 高德地图有起点,想去外省,输入终点(终点正确)不显示也不导航,求大神们... 有什麽可以代替金华火腿的咸鲜味又可节省成本呢??肴汤用途 5千米600米=( )千米 5千米600米等于多少米 5千米600米等于多少米?用小数点,还有5千米60米等于多少米?5千米6米等于多少米? 5.6千米=()千米()米? 5千米6米等于多少千米 比一比5千米600米()5.6千米 5千米600米等于什么 用小数表示 五千米600米减去两千米800米等于多少千米? 600米等于多少千米 34立方分米=______升=______毫升 5千米600米=______千米 7.2平方千米=______公顷. 600米=多少千米 五千米等于多少千米 一片长方形果园长5千米宽600米这片果园的面积是多少平方千米和多少公顷&#65532? 5千米600米=( )米。1小时10分=( )时 三星电视放一会儿就变麻关不了机断电后重启又正常一会儿,反反复复的是怎么回 三星电视打开卡死,还关不了机怎么办? 三星电视用电视开关怎么关机遥控器关不了机 8000米300米-5千米600米等于多少 三星电视怎么关机 5千米70米等于多少千米