找回密码
 立即注册
楼主: 强子

[脚本] 在VS2010编译dll 将链接sql string封装到dll 并且在unity中调用

[复制链接]
发表于 2013-3-5 08:55 | 显示全部楼层 |阅读模式
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Text;
  5. using System.Data.SqlClient;
  6. using System.Data;

  7. namespace connectionDllUnityTest
  8. {
  9.     public class ConnectionTest
  10.     {
  11.         public ConnectionTest()
  12.         {

  13.         }

  14.         public SqlCommand buildCmd(string cmdText, string conStr)
  15.         {
  16.             SqlConnection con = new SqlConnection(conStr);
  17.             SqlCommand cmd = new SqlCommand();
  18.             cmd.Connection = con;
  19.             cmd.CommandText = cmdText;
  20.             return cmd;
  21.         }

  22.         //public Collection<string> returnTest(string cmdText, string conStr)
  23.         //{
  24.         //    Collection<string> result = new Collection<string>();
  25.         //    SqlConnection con = new SqlConnection(conStr);
  26.         //    SqlCommand cmd = new SqlCommand();
  27.         //    cmd.Connection = con;
  28.         //    cmd.CommandText = cmdText;
  29.         //    try
  30.         //    {
  31.         //        con.Open();
  32.         //        SqlDataReader dr = cmd.ExecuteReader();
  33.         //        while (dr.Read())
  34.         //        {
  35.         //            result.Add(dr[0].ToString());
  36.         //        }
  37.         //    }
  38.         //    catch (SqlException ex)
  39.         //    {
  40.         //        return new ApplicationException(ex.Message);
  41.         //    }
  42.         //    finally
  43.         //    {
  44.         //        con.Close();
  45.         //    }

  46.         //}

  47.         public Collection<string> test()
  48.         {
  49.             string conStr1 = @"Data Source=Sql1001.webweb.com,2433;Initial Catalog=DB_98CEEF_test;User Id=DB_98CEEF_test_admin;Password=testtest";
  50.             Collection<string> result = new Collection<string>();
  51.             string cmdText1 = "SELECT * FROM tb_User";
  52.             SqlConnection con = new SqlConnection(conStr1);
  53.             SqlCommand cmd = new SqlCommand();
  54.             cmd.Connection = con;
  55.             cmd.CommandText = cmdText1;

  56.             con.Open();
  57.             SqlDataReader dr = cmd.ExecuteReader();
  58.             while (dr.Read())
  59.             {
  60.                 result.Add(dr[0].ToString());
  61.             }
  62.             dr.Close();
  63.             con.Close();

  64.             return result;
  65.         }
  66.     }
  67. }
复制代码
名为:ConnectionTest.cs
在VS中把.cs生成.dll 首先点解决方案--> 添加--> 新建项目-->类库

在类库的.CS文件编写代码

代码写完后

点解决方案-->生成解决方案

找到其根目录-->Bin文件夹--> Debug文件夹里就是你想要的.DLL文件了

打开unity3d,
写入如下代码:
  1. using UnityEngine;
  2. using System.Collections;
  3. //using connectionDllUnityTest;
  4. using System.Runtime.InteropServices;
  5. using System.Collections.ObjectModel;

  6. public class conDb1 : MonoBehaviour {
  7.     private Collection<string> result = new Collection<string>();
  8.     // Use this for initialization
  9.     void Start ()
  10.     {
  11.         connectionDllUnityTest.ConnectionTest ct = new connectionDllUnityTest.ConnectionTest();
  12.         
  13.         result = ct.test();
  14.     }
  15.    
  16.     // Update is called once per frame
  17.     void Update () {
  18.    
  19.     }
  20.    
  21.     [DllImport("connectionDllUnityTest.dll")]
  22.     private static extern Collection<string> test();
  23.     void OnGUI()
  24.     {
  25.         GUI.Label(new Rect(10, 10, 200, 40), result[0]);
  26.     }
  27. }
复制代码
并引入将编译好的dll引入unity,并引入:i18n.dll、i18n.cjk、i18n.West
如果发布到网页,dll仍然不能编译过去。。。。无解中。痛苦
发表于 2013-4-6 21:17 | 显示全部楼层
{:5_424:}{:5_424:}{:5_424:}{:5_424:}
发表于 2017-3-5 12:18 | 显示全部楼层
很不错
发表于 2017-3-5 11:26 | 显示全部楼层
好帖就是要顶
发表于 2017-3-5 12:05 | 显示全部楼层
顶顶多好
发表于 2017-3-5 11:52 | 显示全部楼层
真心顶
发表于 2017-3-5 11:59 | 显示全部楼层
难得一见的好帖
发表于 2017-3-13 14:07 | 显示全部楼层
楼主是超人
发表于 2017-3-13 14:16 | 显示全部楼层
好帖就是要顶
发表于 2017-3-13 14:38 | 显示全部楼层
顶顶多好
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-3 15:40 , Processed in 0.094686 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表