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# 通过修改图片对象的文件的字节 变相"加密""解密"图片在磁盘的存储
- C#序列化读取和存储文件
- 利用Minidx Extract-Text Com组件从Word,Xls,Pdf……等文件中读取 后附C# 调用代码
- web项目中servlet利用servletContext对象读取资源文件
- Java中读取配置文件中的信息,并利用配置文件中的信息创建对象
- c# 通过修改图片对象的文件的字节 变相"加密""解密"图片在磁盘的存储
- C#读取Mysql blob字段
- blob字段存储文件并读取
- 文件中的每一行存储在 vector容器对象中,读取每行的单词
- c#写对象来读取TXT文本文件
- c# 通过修改图片对象的文件的字节 变相"加密""解密"图片在磁盘的存储
- C#序列化/反序列化对象.将对象写入文件,从文件读取对象.
- 利用sql将存储在 oracle blob中的图片等文件写出
- C# 向数据中存储和读取二进制文件!!
- Python入门的36个例子 之 28 -> Pickle —— 利用文件存储对象
- 向数据库中存储以及读取BLOB对象
- C#读取xml 文件 报错:未将对象引用设置到对象的实例
- C#实现在Sql Server中存储和读取Word文件
- 利用Object 的输入输出流,从文件中读取多个对象