查看: 1133|回复: 1
打印 上一主题 下一主题

在VS2008编译dll 将链接sql string封装到dll 并且在unity中调用

[复制链接]

2508

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32806
精华
12

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-6-15 17:46:32 |只看该作者 |倒序浏览



    using System;

     
    using System.Collections.Generic;

     
    using System.Collections.ObjectModel;

     
    using System.Text;

     
    using System.Data.SqlClient;

     
    using System.Data;

     
   

     
    namespace connectionDllUnityTest

     
    {

     
        public class ConnectionTest

     
        {

     
            public ConnectionTest()

     
            {

     
   

     
            }

     
   

     
            public SqlCommand buildCmd(string cmdText, string conStr)

     
            {

     
                SqlConnection con = new SqlConnection(conStr);

     
                SqlCommand cmd = new SqlCommand();

     
                cmd.Connection = con;

     
                cmd.CommandText = cmdText;

     
                return cmd;

     
            }

     
   

     
            //public Collection<string> returnTest(string cmdText, string conStr)

     
            //{

     
            //    Collection<string> result = new Collection<string>();

     
            //    SqlConnection con = new SqlConnection(conStr);

     
            //    SqlCommand cmd = new SqlCommand();

     
            //    cmd.Connection = con;

     
            //    cmd.CommandText = cmdText;

     
            //    try

     
            //    {

     
            //        con.Open();

     
            //        SqlDataReader dr = cmd.ExecuteReader();

     
            //        while (dr.Read())

     
            //        {

     
            //            result.Add(dr[0].ToString());

     
            //        }

     
            //    }

     
            //    catch (SqlException ex)

     
            //    {

     
            //        return new ApplicationException(ex.Message);

     
            //    }

     
            //    finally

     
            //    {

     
            //        con.Close();

     
            //    }

     
   

     
            //}

     
   

     
            public Collection<string> test()

     
            {

     
                string conStr1 = @"Data Source=Sql1001.webweb.com,2433;Initial Catalog=DB_98CEEF_test;User Id=DB_98CEEF_test_adminassword=testtest";

     
                Collection<string> result = new Collection<string>();

     
                string cmdText1 = "SELECT * FROM tb_User";

     
                SqlConnection con = new SqlConnection(conStr1);

     
                SqlCommand cmd = new SqlCommand();

     
                cmd.Connection = con;

     
                cmd.CommandText = cmdText1;

     
   

     
                con.Open();

     
                SqlDataReader dr = cmd.ExecuteReader();

     
                while (dr.Read())

     
                {

     
                    result.Add(dr[0].ToString());

     
                }

     
                dr.Close();

     
                con.Close();

     
   

     
                return result;

     
            }

     
        }

     
    }


复制代码
名为:ConnectionTest.cs

在VS中把.cs生成.dll 首先点解决方案--> 添加--> 新建项目-->类库



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



代码写完后



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



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



打开unity3d

写入如下代码:



    using UnityEngine;

     
    using System.Collections;

     
    //using connectionDllUnityTest;

     
    using System.Runtime.Interopservices;

     
    using System.Collections.ObjectModel;

     
   

     
    public class conDb1 : MonoBehaviour {

     
            private Collection<string> result = new Collection<string>();

     
            // Use this for initialization

     
            void Start ()

     
            {

     
                    connectionDllUnityTest.ConnectionTest ct = new connectionDllUnityTest.ConnectionTest();

     
                    

     
                    result = ct.test();

     
            }

     
            

     
            // Update is called once per frame

     
            void Update () {

     
            

     
            }

     
            

     
            [DllImport("connectionDllUnityTest.dll")]

     
            private static extern Collection<string> test();

     
            void OnGUI()

     
            {

     
                    GUI.Label(new Rect(10, 10, 200, 40), result[0]);

     
            }

     
    }


复制代码
并引入将编译好的dll引入unity,并引入:i18n.dll、i18n.cjk、i18n.West

如果发布到网页,dll仍然不能编译过去。。。。无解中。痛苦
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-12-30 04:13:14 |只看该作者
学习了,虽然还是有难度,谢谢楼主的用心
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-23 06:08 , Processed in 0.100010 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部