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

新建一个场景,此脚本挂着相机上,同时提前在网站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;
    //    }
    //}
}
即将访问的数据库结构为:

运行中图为:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LuckyDog阿祥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值