从GBASE南大通用数据库中读取BLOB 数据写入到一个文件

下面的代码从上一节GBASE南大通用数据库中创建的 file表中获得一行,并将数据写入到GBASE南大通用的文件中。

C# 示例:

using System;

using System.IO;

using GBase.Data.GBaseClient;

namespace UsingGBase

{class Program

{

static void Main(string[] args)

{

GBaseDataReader gsData;

GBaseConnection conn = new GBaseConnection();

GBaseCommand cmd = new GBaseCommand();

string SQL;

int FileSize;

byte[] rawData;

FileStream fs;

conn.ConnectionString =

"server=192.168.5.41;uid=root;pwd=1;database=test;pooling=false";

SQL = "SELECT file_name, file_size, file FROM file";

try

{

conn.Open();

cmd.Connection = conn;

cmd.CommandText = SQL;

gsData = cmd.ExecuteReader();

if (!gsData.HasRows)

throw new Exception("There are no BLOBs to

save");

gsData.Read();

FileSize =

(int)gsData.GetUInt32(gsData.GetOrdinal("file_size"));

rawData = new byte[FileSize];

gsData.GetBytes(gsData.GetOrdinal("file"), 0,

rawData, 0, FileSize);

fs = new FileStream(@"C:\newfile.png",

FileMode.OpenOrCreate, FileAccess.Write);

fs.Write(rawData, 0, FileSize);

fs.Close();

Console.WriteLine("File successfully written to

disk!");

gsData.Close();

conn.Close();

}

catch (GBaseException ex)

{

Console.WriteLine("Error " + ex.Number + " has

occurred: " + ex.Message);

}

}

}

}

你可能感兴趣的:(数据库,GBASE南大通用,GBASE)