Home Database Mysql Tutorial C#实现MySQL数据库中的blob数据存储_MySQL

C#实现MySQL数据库中的blob数据存储_MySQL

May 30, 2016 pm 05:10 PM
c# data database

在MySQL数据库中,有一种blob数据类型,用来存储文件。C#编程语言操作MySQL数据库需要使用MySQL官方组件MySQL.Data.dll。

 

Mysql.Data.dll(6.9.6)组件下载地址:http://download.csdn.net/detail/keypig_zz/9262767。

 

现在说一说如何实现blob类型数据的操作。

 

新建winform程序,添加两个按钮。代码如下:

 

 1 System.IO.MemoryStream ms = new System.IO.MemoryStream();
 2 private void button1_Click(object sender, EventArgs e)
 3 {
 4 //测试序列化与反序列化
 5 int[] arr = { 1, 2, 3 };
 6 BinaryFormatter bFormatter = new BinaryFormatter();
 7 bFormatter.Serialize(ms, arr);
 8 byte[] byteArr = ms.ToArray();
 9 MessageBox.Show(byteArr.Length.ToString());
10 MySqlConnection conn = new MySqlConnection(Properties.Settings.Default.MySqlConnectString);
11 //string insertStr = "update mm set aa=4,arr=@blobData where aa=4;";
12 string insertStr = "insert into mm(arr) values(@blobData);";//需要主键设置自增
13 MySqlParameter par=new MySqlParameter("@blobData",MySqlDbType.Blob);
14 par.Value=byteArr;
15 MySqlCommand cmd = new MySqlCommand(insertStr, conn);
16 cmd.Parameters.Add(par);
17 try
18 {
19 conn.Open();
20 cmd.ExecuteNonQuery();
21 ms.Close();
22 ms.Dispose();
23 }
24 catch (Exception ep)
25 {
26 MessageBox.Show(ep.Message);
27 }
28 
29 
30 }
31 
32 private void button2_Click(object sender, EventArgs e)
33 {
34 BinaryFormatter bFormatter = new BinaryFormatter();
35 
36 MySql.Data.MySqlClient.MySqlDataReader myData;
37 MySqlConnection  conn = new MySql.Data.MySqlClient.MySqlConnection(Properties.Settings.Default.MySqlConnectString);
38 string readStr = "select arr from mm where id =6;";
39 MySqlCommand cmd = new MySqlCommand(readStr, conn);
40 try
41 {
42 conn.Open();
43 myData = cmd.ExecuteReader();
44 if (!myData.HasRows)
45 {
46 throw new Exception("没有blob数据");
47 }
48 myData.Read();
49 byte[] blob = new byte[myData.GetBytes(0, 0, null, 0, int.MaxValue)];
50 myData.GetBytes(0, 0, blob, 0, blob.Length);
51 myData.Close();
52 ms = new System.IO.MemoryStream(blob);
53 ms.Position = 0;
54 int[] arr = (int[])bFormatter.Deserialize(ms);
55 ms.Dispose();
56 string arrStr = null;
57 for (int i = 0; i < arr.GetLength(0); i++)
58 {
59 arrStr += arr[i].ToString()+" ";
60 }
61 MessageBox.Show(arrStr);
62 }
63 catch(Exception ep)
64 {
65 MessageBox.Show(ep.Message);
66 }
67 
68 } 
Copy after login

 

 

代码中的连接字符串存储于Settings中,具体格式为:“server=127.0.0.1;user=root;database=***;port=3306;password=***;”。数据库中存在叫做mm的一个表格,含有一个id字段(主键,自增),一个arr字段(blob类型)。

 

上述代码将一个整型数组{1,2,3}序列化之后以blob数据类型存储在MySQL数据库中,进一步,又从MySQL数据库中读取blob数据,进行反序列化,得到整型数组{1,2,3}。代码执行结果如下:
C#实现MySQL数据库中的blob数据存储_MySQL


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1652
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to convert xml into word How to convert xml into word Apr 03, 2025 am 08:15 AM

There are three ways to convert XML to Word: use Microsoft Word, use an XML converter, or use a programming language.

See all articles