您的位置:首页 > 数据库 > MySQL

C#利用MySQL的BLOB存储和读取文件和对象

2016-11-03 14:55 736 查看

BLOB类型

在MySQL中,BLOB类型的字段用于存储二进制数据

BLOB类型分为四类(单位:字节)

TinyBlob 最大 255

Blob 最大 65K

MediumBlob 最大 16M

LongBlob 最大 4G

WPF实现

在WPF的窗口新建四个按钮,功能如图所示:



存文件到数据库

private void Button_Click(object sender, RoutedEventArgs e)
{
MySqlConnection conn;
MySqlCommand cmd;

conn = new MySqlConnection();
cmd = new MySqlCommand();

string SQL;
long FileSize;
byte[] rawData;
FileStream fs;

conn.ConnectionString = "SERVER=120.95.132.137;DATABASE=cad_project;UID=root;PASSWORD=;charset=utf8;";

try
{
fs = new FileStream(@"f:\test1.txt", FileMode.Open, FileAccess.Read);
FileSize = fs.Length;

rawData = new byte[FileSize];
fs.Read(rawData, 0, (int)FileSize);
fs.Close();

conn.Open();

SQL = "INSERT INTO serialization VALUES(NULL, @FileName, @FileSize, @File)";

cmd.Connection = conn;
cmd.CommandText = SQL;
cmd.Parameters.AddWithValue("@FileName", "test1");
cmd.Parameters.AddWithValue("@FileSize", FileSize);
cmd.Parameters.AddWithValue("@File", rawData);

cmd.ExecuteNonQuery();

MessageBox.Show("File Inserted into database successfully!",
"Success!");

conn.Close();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show("Error " + ex.Number + " has occurred: " + ex.Message,
"Error");
}
}


读取数据库文件

private void Button_Click_1(object sender, RoutedEventArgs e)
{
MySqlConnection conn;
MySqlCommand cmd;
MySqlDataReader myData;

conn = new MySqlConnection();
cmd = new MySqlCommand();

string SQL;
UInt32 FileSize;
byte[] rawData;
FileStream fs;

conn.ConnectionString = "SERVER=120.95.132.137;DATABASE=cad_project;UID=root;PASSWORD=;charset=utf8;";

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

try
{
conn.Open();

cmd.Connection = conn;
cmd.CommandText = SQL;

myData = cmd.ExecuteReader();

if (!myData.HasRows)
throw new Exception("There are no BLOBs to save");

myData.Read();

FileSize = myData.GetUInt32(myData.GetOrdinal("file_size"));
rawData = new byte[FileSize];

myData.GetBytes(myData.GetOrdinal("file"), 0, rawData, 0, (int)FileSize);

fs = new FileStream(@"F:\test2.txt", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(rawData, 0, (int)FileSize);
fs.Close();

MessageBox.Show("File successfully written to disk!",
"Success!");

myData.Close();
conn.Close();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show("Error " + ex.Number + " has occurred: " + ex.Message,
"Error");
}
}


存储对象到数据库

private void Button_Click_2(object sender, RoutedEventArgs e)
{
ArrayList al = new ArrayList();
al.Add(123.45);
al.Add(378.93);
al.Add(451.32);

MemoryStream ms = new MemoryStream();
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(ms, al);
byte[] byteArr = ms.ToArray();

MySqlConnection conn;
MySqlCommand cmd;

conn = new MySqlConnection();
cmd = new MySqlCommand();

string SQL;

conn.ConnectionString = "SERVER=120.95.132.137;DATABASE=cad_project;UID=root;PASSWORD=;charset=utf8;";

try
{

conn.Open();

SQL = "INSERT INTO serialization VALUES(NULL, @FileName, @FileSize, @File)";

cmd.Connection = conn;
cmd.CommandText = SQL;
cmd.Parameters.AddWithValue("@FileName", "arraylist");
cmd.Parameters.AddWithValue("@FileSize", byteArr.Length);
cmd.Parameters.AddWithValue("@File", byteArr);

cmd.ExecuteNonQuery();

MessageBox.Show("File Inserted into database successfully!",
"Success!");

conn.Close();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show("Error " + ex.Number + " has occurred: " + ex.Message,
"Error");
}

}


从数据库读取对象

private void Button_Click_3(object sender, RoutedEventArgs e)
{
//从数据库读取对象
BinaryFormatter bFormatter = new BinaryFormatter();

MySql.Data.MySqlClient.MySqlConnection conn;
MySql.Data.MySqlClient.MySqlCommand cmd;
MySql.Data.MySqlClient.MySqlDataReader myData;

conn = new MySql.Data.MySqlClient.MySqlConnection();
cmd = new MySql.Data.MySqlClient.MySqlCommand();

string SQL;
UInt32 FileSize;
byte[] rawData;
FileStream fs;

conn.ConnectionString = "SERVER=120.95.132.137;DATABASE=cad_project;UID=root;PASSWORD=;charset=utf8;";

SQL = "SELECT file_name, file_size, file FROM serialization where file_name = 'arraylist'";

try
{
conn.Open();

cmd.Connection = conn;
cmd.CommandText = SQL;

myData = cmd.ExecuteReader();

if (!myData.HasRows)
throw new Exception("There are no BLOBs to save");

myData.Read();

FileSize = myData.GetUInt32(myData.GetOrdinal("file_size"));
rawData = new byte[FileSize];

myData.GetBytes(myData.GetOrdinal("file"), 0, rawData, 0, (int)FileSize);

MemoryStream ms = new MemoryStream(rawData);
ms.Position = 0;
ArrayList al = (ArrayList)bFormatter.Deserialize(ms);
ms.Dispose();

foreach(Object o in al){
Console.WriteLine((double)o);
}

MessageBox.Show("File successfully written to disk!",
"Success!");

myData.Close();
conn.Close();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show("Error " + ex.Number + " has occurred: " + ex.Message,
"Error");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql blob c# wpf 序列化