c# mysql 批量存储图片_c# 如何向mysql数据库中存入图片

本文介绍如何使用C#将图片批量存储到MySQL数据库中,并演示了从数据库中读取和显示图片的方法。通过FileStream读取图片文件,然后使用BinaryReader转换为字节流,最后利用OleDbCommand执行插入操作。读取时,根据编号查询数据库,将图片数据流转化为Image对象并在pictureBox中展示。
摘要由CSDN通过智能技术生成

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

private void button2_Click(object sender, EventArgs e)

{

openFileDialog1.Filter = "*.jpg|*.jpg|*.gif|*.gif|*.BMP|*.BMP";

if (openFileDialog1.ShowDialog() == DialogResult.OK)

{

string fullpath = openFileDialog1.FileName;

FileStream fs = new FileStream(fullpath, FileMode.Open);

byte[] imagebytes = new byte[fs.Length];

BinaryReader br = new BinaryReader(fs);

imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));

string strpath = Application.StartupPath + "\\b.mdb";//b.mdb a.xls

string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strpath;

OleDbConnection olecon = new OleDbConnection(constr);

olecon.Open();

OleDbCommand oledcom = new OleDbCommand("insert into 表4(图片) values(@ImageList)", olecon);

oledcom.Parameters.Add("ImageList", OleDbType.Binary);

oledcom.Parameters["ImageList"].Value = imagebytes;

oledcom.ExecuteNonQuery();

olecon.Close();

MessageBox.Show("图片保存完毕");

}

}

//上面是保存,下面是读取

private void pictureBox1_Click(object sender, EventArgs e)

{

int a = Convert.ToInt32(textBox1.Text);

a ++;

if (a > Convert.ToInt32(textBox2.Text))

{

a = a % Convert.ToInt32(textBox2.Text) + 3;

}

textBox1.Text = a.ToString();

string strpath = Application.StartupPath + "\\b.mdb";

string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strpath;

OleDbConnection conn = new OleDbConnection(constr);

string strSql = "select 图片 from 表4 where 编号 LIKE '" + textBox1.Text + "'"; OleDbCommand cmd = new OleDbCommand(strSql, conn); conn.Open();

OleDbDataReader reader = cmd.ExecuteReader();

reader.Read();

MemoryStream ms = new MemoryStream((byte[])reader["图片"]); //创建一个内存读取数据流,将读取的数据库的图片以二进制的byte[]流存入内存中。 reader.Close();

conn.Close();

pictureBox1.Image = Image.FromStream(ms, true); //从刚才存储到内存的数据流中创建Image对象。

您可以使用OPC UA的客户端库来连接和读取OPC服务器的数据。以下是一个基本的C#代码示例: ```csharp using Opc.Ua; using Opc.Ua.Client; using MySql.Data.MySqlClient; // 建立连接 var endpointUrl = "opc.tcp://localhost:4840"; // OPC服务器的URL var endpoint = new EndpointDescription(endpointUrl); var configuration = EndpointConfiguration.Create(); var endpointInstance = new ConfiguredEndpoint(null, endpoint, configuration); var session = Session.Create( configuration, endpointInstance, false, "", 60000, new UserIdentity(new AnonymousIdentityToken()), null).Result; // 读取数据 var nodeId = "ns=2;s=MyVariable"; // 要读取的变量节点ID var node = (VariableNode)session.ReadNode(ExpandedNodeId.Parse(nodeId)); var value = (DataValue)session.ReadValue(node.NodeId); Console.WriteLine("Value: " + value.Value.ToString()); // 将数据插入MySQL数据库 string connectionString = "Server=localhost;Database=myDatabase;Uid=myUsername;Pwd=myPassword;"; using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); string query = "INSERT INTO myTable (value) VALUES (@value)"; MySqlCommand command = new MySqlCommand(query, connection); command.Parameters.AddWithValue("@value", value.Value.ToString()); command.ExecuteNonQuery(); } ``` 在上面的示例,首先建立了与OPC服务器的连接,然后读取了一个变量节点的值,并将其插入MySQL数据库。您需要将代码的节点ID和数据库连接字符串替换为您自己的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值