多米诺 发表于 2012-11-26 22:18

ASP.net+Unity3D完美实现数据库操作方法(已测通过各个平台)

http://www.u3dchina.com/forum.php?mod=viewthread&tid=323
很多同学用到这样方法导出时无法实现操作数据库,由于U3D对dll支持并不是很完美,这里给出一个交给web端来处理的详细教程可以解决各个平台操作mssql的问题。php、mysql等同理
接下来我们通过在数据库中插入字段来讲解此教程。
1、打开mssql 创建一个表tb_Messagecreate table tb_Message
(
guest_id int IDENTITY(1,1) primary key,
guest_name nvarchar(20),
guest_score nvarchar(255),
guest_message nvarchar(255),
)注:nvarchar类型支持中文字符



2、打开vs新建一个asp.net的网站
web.config配置如下:
注:如有防火墙请填写端口号<?xml version="1.0"?>

<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->

<configuration>
<appSettings>
    <add key="connString" value="Data Source=Sql1001.u3dchina.com,2433;Initial Catalog=DB_98CEEF_test;User Id=DB_98CEEF_test_admin;Password=testtest;"/>
</appSettings>
<system.web>
    <compilation debug="true" targetFramework="4.0" />
</system.web>

</configuration>这里定义数据库连接字符串
为了方便数据操作,新建一个app_code(自带的),添加dbhelper
在对应的.aspx.cs中添加如下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    DbHelper help = new DbHelper();
    protected void Page_Load(object sender, EventArgs e)
    {
      string guest_name=Request.Form["post_name"];
      string guest_score=Request.Form["post_score"];
      string guest_message=Request.Form["post_message"];
      string insertsql = "insert into tb_Message (guest_name,guest_score,guest_message) values('" + guest_name + "','"+guest_score+"','"+guest_message+"')";
      if (help.Exists(selectsql))
      {
            Response.Write("have the name!");

      }
      else
      {
            Response.Write("Don't have the name!");
            string insertsql = "insert into tb_Message (guest_name,guest_score,guest_message) values('nvchar测试姓名','varchar测试分数',N'text带n测试')";
            if (help.ExecuteSqlInt(insertsql) > 0)
                Response.Write("Insertsuccess");
            else
                Response.Write("Insertfailed");
      }
      
    }
}原理:通过form临时数据存取提取unity3d的中的form请求,在网页中接受值并对数据库进行操作
右键在浏览器中查看
显示:Don't have the name!Insertsuccess
检测结果:正常进入网络mssql中可以查看到这些数据
三、在u3d中创建一个c#脚本:
代码如下:
using UnityEngine;
using System.Collections;

public class insert : MonoBehaviour {
      private string url="http://localhost:50163/WebSite2/Default.aspx";//本地地址
      private string txt_Name="";
      private string txt_Score="";
      private string txt_Message="";
      // Use this for initialization
      void Start () {
      
      }
      
      // Update is called once per frame
      void Update () {
      
      }
      void OnGUI()
      {
                txt_Name=GUI.TextField(new Rect(10,10,100,30),txt_Name);
                txt_Score=GUI.TextField(new Rect(10,50,100,30),txt_Score);
                txt_Message=GUI.TextField(new Rect(10,90,100,30),txt_Message);
                if(GUI.Button(new Rect(10,130,100,30),"submit"))
                {
                        StartCoroutine(InsertMethod());
                }
      }
      IEnumerator InsertMethod()
      {
                WWWForm form=new WWWForm();
                form.AddField("post_name",txt_Name);
                form.AddField("post_score",txt_Score);
                form.AddField("post_message",txt_Message);
                WWW w=new WWW(url,form);
         yield return w;
                print (w.text);
               
      }
}绑定运行,查看print结果,查看数据库插入成功或提示已存在!

如上图的积分排名就是此种方式实现的

多米诺 发表于 2012-11-26 22:22

希望大家多多发布教程 共同探讨{:soso_e100:}

show19880805 发表于 2012-11-28 09:36

挺好的你这生成以后没啥问题吧, 我以前弄的一生成 WEB 版的就 完蛋。。。顶你下

noflyzone 发表于 2012-11-28 13:24

不用客户端的数据吗?这样会不会用加密数据

ゅ星星点灯° 发表于 2012-11-28 16:29

不错谢谢分享{:5_427:}

心梦无痕 发表于 2013-4-7 23:14


不错 不错 不错{:soso__3922851084632044791_6:}

starwallace 发表于 2013-5-3 10:59


不错 不错 不错{:soso__3922851084632044791_6:}

earthn2000 发表于 2013-5-8 23:04

這個好,我要了

gogogodeng 发表于 2013-5-25 19:27

好厉害的样子

我没有过去 发表于 2013-5-25 21:16

感谢分享{:5_399:}
页: [1] 2 3 4 5 6 7
查看完整版本: ASP.net+Unity3D完美实现数据库操作方法(已测通过各个平台)