C# 使用 ADO.NET 将二进制数据流保存到 MySQL 数据库
使用 ADO.NET 将二进制数据流保存到 MySQL 数据库
以下是使用 ADO.NET 实现将二进制数据流保存到 MySQL 数据库的完整代码示例:
using MySql.Data.MySqlClient;
using System.Data;
private void SaveToDatabase(byte[] imageData)
{
string connectionString = 'Your MySQL Connection String';
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
// 创建一个MySQL命令对象
MySqlCommand command = new MySqlCommand();
command.Connection = connection;
command.CommandType = CommandType.Text;
// 设置SQL语句,使用参数化查询
command.CommandText = 'INSERT INTO YourTable (ImageData) VALUES (@ImageData)';
// 创建并添加一个参数
MySqlParameter parameter = new MySqlParameter('@ImageData', MySqlDbType.Blob);
parameter.Value = imageData;
command.Parameters.Add(parameter);
// 执行SQL命令
command.ExecuteNonQuery();
}
}
请确保将 'Your MySQL Connection String' 替换为您的 MySQL 连接字符串,并将 'YourTable' 替换为您要插入数据的表的名称。
原文地址: https://www.cveoy.top/t/topic/zit 著作权归作者所有。请勿转载和采集!