C# 将图片插入 Ms Access

C# Insert Picture into Ms Access(C# 将图片插入 Ms Access)
本文介绍了C# 将图片插入 Ms Access的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我要感谢所有在上一个问题中提供帮助的人.但现在,我对另一种说法有问题,即 saveimageto MS 访问.首先我想问一下,在ms access数据库上,Datatype应该放附件吗?

I would like to thanks to all who helped in last question. but now, i have problem with another statement which is saveimageto MS access. First, I would like to ask, on ms access database, Datatype should put attachment?

我的代码:

private void button2_Click(object sender, EventArgs e)
        {

            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into Table1 (id,picture) values ('" + textBox1.Text +  "')";

            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            System.Windows.Forms.MessageBox.Show("Created", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            con.Close();

        }

我使用 openFIledialog 将我的图片插入图片框.

I insert my picture to picturebox using openFIledialog.

推荐答案

首先,使用参数.永远不要连接 SQL 命令的字符串,因为它会向 SQL 注入开放.这是一个易于遵循的良好做法,可以避免您将来遇到很多麻烦.

First, use parameters. NEVER concat strings for SQL commands, as it opens itself to SQL Injection. That's an easy to follow good practice that'll avoid you a lot of trouble in the future.

也就是说,这样的事情应该可以工作:

That said, something like this should work:

// You've got the filename from the result of your OpenDialog operation
var pic = File.ReadAllBytes(yourFileName);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into Table1 (id, picture) values (@p1, @p2)";
cmd.Parameters.AddWithValue("@p1", TextBox1.Text);
cmd.Parameters.AddWithValue("@p2", pic);
cmd.ExecuteNonQuery();

在这里从记忆中引用,但如果该代码给您带来麻烦,请告诉我.如果我没记错的话,应该可以这样.

Citing from memory here, but let me know if that code gives you trouble. If I remember correctly, something like that should work.

EDIT.- 如果您在 PictureBox 控件上预加载图像,将该图像转换为字节数组,然后将该字节数组用作第二个参数.

EDIT.- If you're preloading the image on a PictureBox control, convert that image to a byte array and then use that byte array as the second parameter.

编辑 (2).- 稍微澄清一下.如果您要从文件中获取图像,那么您就有了它的路径;那么你可以使用 File.ReadAllBytes(string path).在我的示例中,我假设 yourFileName 是成功 OpenDialog 操作后所选文件的文件和路径名.所以你可以这样使用它:

EDIT (2).- A little clarification. If you're are getting your image from a file, you have a path to it; then you can use File.ReadAllBytes(string path). In my example I was assuming that yourFileName was the file and path name of the selected file after a successful OpenDialog operation. So you can use it like this:

byte[] fromPath = File.ReadAllBytes(@"C:wallsaurora.jpg");

然后您将图像存储到字节数组 fromPath 中,转换为字节并准备好在插入命令中使用,如上所示.

and you'd store into the byte array fromPath the image, converted to bytes and ready to be used in an insertion command as seen above.

但是如果您从图片框控件获取图像,情况会有些不同:

BUT if you're getting your image from a picture box control, things are a little different:

MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, Imaging.ImageFormat.Jpeg);
byte[] fromControl = ms.GetBuffer();

在该示例中,我创建了一个 MemoryStream,用图片框控件的内容填充它,然后将其传递给字节数组,我们再次准备将其用作参数用于我们的插入查询.

In that example I've created a MemoryStream, filled it with the content of the picturebox control and then passed it to the byte array, and again we're ready to use it as a parameter for our insertion query.

哦,别忘了加

using System.IO;
using System.Drawing;

到你的使用.

这篇关于C# 将图片插入 Ms Access的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Custom Error Queue Name when using EasyNetQ for RabbitMQ?(使用 EasyNetQ for RabbitMQ 时自定义错误队列名称?)
How to generate password_hash for RabbitMQ Management HTTP API(如何为 RabbitMQ 管理 HTTP API 生成密码哈希)
Rabbitmq Ack or Nack, leaving messages on the queue(Rabbitmq Ack 或 Nack,将消息留在队列中)
Setup RabbitMQ consumer in ASP.NET Core application(在 ASP.NET Core 应用程序中设置 RabbitMQ 消费者)
Specify Publish timeouts in mass transit(指定公共交通中的发布超时)
RabbitMQ asynchronous support(RabbitMQ 异步支持)