- 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_admin;Password=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仍然不能编译过去。。。。无解中。痛苦
|