C#中插入图片到Access女士 [英] C# Insert Picture into Ms Access
问题描述
我想感谢所有谁在最后一个问题有帮助。
,但现在,我有问题,这是saveimageto MS访问的另一个说法。 ?
首先,我想问一下,MS Access数据库,数据类型应该把附件
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.
推荐答案
首先,使用的参数。 NEVER CONCAT字符串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(字符串路径)
。在我的例子,我假设 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:\walls\aurora.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 如果您是从一个图片框控件让你的形象,事情有一点不同:
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
,与PictureBox控件的内容填写,然后将其传递到字节数组,并再次我们准备用它作为我们插入查询参数。
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;
您usings。
to your usings.
这篇关于C#中插入图片到Access女士的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!