如果你的輸出結果與放入前的數值不一樣,請先看這篇文章

http://lolikitty.pixnet.net/blog/post/185218848

程式碼:

using UnityEngine;
using System.Collections;
using Npgsql;

public class TestPgSQL : MonoBehaviour {

    void Start () {
        // 登入資料庫,請依造你的資料庫設定
        NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=postgres;Password=a;Database=test;");
        conn.Open();

        NpgsqlCommand command = new NpgsqlCommand();
        command.Connection = conn;

        command.CommandText = "delete from file;"; // 先清除之前表格全部的資料
        command.ExecuteNonQuery();

        command.CommandText= "insert into file(id, data) values (0,:bytesData);"; // 放入 byte[] 的 SQL 語法
        NpgsqlParameter param = new NpgsqlParameter(":bytesData", NpgsqlTypes.NpgsqlDbType.Bytea);

        byte [] b = new byte[3]; // 這是要放入資料庫的資料
        b[0] = 100;
        b[1] = 200;
        b[2] = 255;

        param.Value = b;
        command.Parameters.Add(param);
        command.ExecuteNonQuery(); // 放入資料庫

        command.CommandText = "select * from file;"; // 查詢表格

        NpgsqlDataReader dr = command.ExecuteReader();

        while(dr.Read()) {
            string s = "";
            int bl = ((byte[])dr.GetValue(1)).Length;
            for(int i = 0; i<bl; i++){
                s += ((byte[])dr.GetValue(1))[i] + ", ";
            }
            print (s);
            print (bl);
        }

        conn.Close();
    }

}


輸出結果:

Unity - A.unity - New Unity Project 24 - PC, Mac & Linux Standalone DX11 on DX10 GPU  




arrow
arrow
    全站熱搜

    黃彥霖 發表在 痞客邦 留言(0) 人氣()