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; // } //} }
即将访问的数据库结构为:
运行中图为:
相关文章推荐
- 使用PHP向Mysql数据库插入信息,页面中文内容显示正常,但在数据库里却是乱码,这个怎么解决?
- 完整的解决oracle乱码shell处理(当每日导入数据库是英文字符集但需要导入中文字符集并且显示中文)
- 织梦dedecms后台发布文章提示“标题不能为空”,编辑器内容无法显示中文
- Linux技巧--Redhat 5系统中的vim编辑器无法正常显示中文解决方案(中文显示为乱码)/Ubuntu 12.04系统gedit中文乱码
- SqlServer更新中文数据库显示乱码,更新内容有单引号
- 在php页面中显示数据库内容时,中文显示乱码
- 关于页面中文正常显示,存到数据库乱码的问题
- php mysql 中文乱码解决,数据库显示正常,php调用不正常
- VS2005 C++MFC 数据库返回中文数据 显示时乱码
- 获取一个网页数据返回的编码类型是gzip,解压后,网页中包含的中文字段变成了乱码,只需要把编码更改为BIG5 ,繁体字就正常显示了!
- vs2005 ajax发布网站,中文获取值为乱码,并且IIS中运行ASPX时服务器应用程序不可用-解决
- XML输出中文时,无法用xsl查看(XML文件不能正常显示、中文显示乱码)
- Ubuntu9.04太多乱码(中文不能正常显示)
- struct+hibernate框架,jsp提交到后台,存到数据库里中文显示乱码问题。
- 20100722 使用FTP客户端(filezilla)中文目录显示乱码无法正常使用解决
- web中,如何读取Word内容(包含表格,但不包含图片)并且显示在页面中或保存进数据库中
- jfreechart图,windows下中文显示正常,linux下乱码
- MYSQL与PHP链接,前台显示编码正常,后台乱码
- php调用mysql数据库,但数据库不能显示中文呈乱码
- PHP MySQL中文内容显示乱码