您的位置:首页 > 移动开发 > Unity3D

unity3d链接postgresql,读数据库内容并且显示(读出的中文在编辑器中乱码,发布standalone正常)

2016-12-01 15:51 666 查看
新建一个场景,此脚本挂着相机上,同时提前在网站http://blog.csdn.net/FlashDragon/article/details/4789980准备工作(如导入Npgsql.dll和System.Data.dll)
using UnityEngine;
using Npgsql;
using System.Data;

public class postgresql : MonoBehaviour
{
string strConnec;
string strSelect;
string strResult;
NpgsqlConnection conn;

/// <summary>
/// 静态读取txt
/// </summary>
public TextAsset txtYxx;
string strYxx;
/// <summary>
/// Resources
/// </summary>
string strYtext;

// Use this for initialization
void Start()
{
//strYxx = txtYxx.text;
//print("读取的yxx.txt内容为:"+strYxx);

//strYtext = ((TextAsset)Resources.Load("ytest")).text;
//print(strYtext);

try
{
//192.168.1.222
strConnec = "Server=192.168.1.234;Port=5432;User Id=postgres;Password=root;Database=gafk";
//strConnec = "Server=192.168.1.222;Port=5432;User Id=postgres;Password=root;Database=postgres";
// strConnec = "Server=192.168.1.123;Port=5432;User Id=postgres;Password=root;Database=lucky;"; //charset ='utf8'";
conn = new NpgsqlConnection(strConnec);
conn.Open();
if (conn.State == ConnectionState.Open)
{
print("Connection is on!");

//strSelect = "SELECT mc FROM zydd  WHERE \"id\"='4028816a57e025bc0157e0c18b8c0027'";//单行查询
strSelect = "SELECT mc,dm FROM zyll_local  WHERE xxlb='8'"; //多行查询

//strSelect = "select name from luck where id='1'";
// strSelect = "select name from yxx where id ='1'";
}
}
catch (System.Exception ex)
{
print(ex);
}
}
void OnGUI()
{
GUILayout.Label(" ");
if (GUILayout.Button("connect to server"))
{
DataSet ds = new DataSet();
NpgsqlDataAdapter da = new NpgsqlDataAdapter(strSelect, conn);
da.Fill(ds);
foreach (DataTable table in ds.Tables)
{
print("ds.Tables.Count;" + ds.Tables.Count);
foreach (DataRow row in table.Rows)
{
print("table.Rows.Count;" + table.Rows.Count);
foreach (DataColumn column in table.Columns)
{
print("table.Columns.Count;" + table.Columns.Count);

print(" 第1张表第2行2列数据是:" + ds.Tables[0].Rows[1][1]);

//strResult = row[column].ToString();
//print("1:" + row[column].ToString());
//print();
}
}
}
}
GUILayout.Label(strResult);
}
/*
* 解决unity3d读写中文乱码
//http://www.tuicool.com/articles/BriMR3
* */
//public void Read()
//{
//    try
//    {
//        string pathSource = "test.txt";
//        using (FileStream fsSouce = new FileStream(pathSource, FileMode.Open, FileAccess.Read))
//        {
//            byte[] bytes = new byte[fsSouce.Length];
//            int numBytesToRead = (int)fsSouce.Length;
//            int numbytesRead = 0;
//            while (numBytesToRead>0)
//            {
//                int n = fsSouce.Read(bytes, numbytesRead, numBytesToRead);
//                if (n==0)
//                {
//                    break;
//                }
//                numbytesRead += n;
//                numBytesToRead -= n;
//            }
//            numBytesToRead = bytes.Length;
//           string str= UTF8Encoding.UTF8.GetString(bytes);
//        }
//    }
//    catch (System.Exception)
//    {

//        throw;
//    }
//}
}
即将访问的数据库结构为:
运行中图为:

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐