問題描述
我要感謝所有在上一個(gè)問題中提供幫助的人.但現(xiàn)在,我對另一種說法有問題,即 saveimageto MS 訪問.首先我想問一下,在ms access數(shù)據(jù)庫上,Datatype應(yīng)該放附件嗎?
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.
推薦答案
首先,使用參數(shù).永遠(yuǎn)不要連接 SQL 命令的字符串,因?yàn)樗鼤?huì)向 SQL 注入開放.這是一個(gè)易于遵循的良好做法,可以避免您將來遇到很多麻煩.
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.
也就是說,這樣的事情應(yīng)該可以工作:
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();
在這里從記憶中引用,但如果該代碼給您帶來麻煩,請告訴我.如果我沒記錯(cuò)的話,應(yīng)該可以這樣.
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 控件上預(yù)加載圖像,將該圖像轉(zhuǎn)換為字節(jié)數(shù)組,然后將該字節(jié)數(shù)組用作第二個(gè)參數(shù).
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)
.在我的示例中,我假設(shè) 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");
然后您將圖像存儲到字節(jié)數(shù)組 fromPath 中,轉(zhuǎn)換為字節(jié)并準(zhǔn)備好在插入命令中使用,如上所示.
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.
但是如果您從圖片框控件獲取圖像,情況會(huì)有些不同:
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();
在該示例中,我創(chuàng)建了一個(gè) MemoryStream
,用圖片框控件的內(nèi)容填充它,然后將其傳遞給字節(jié)數(shù)組,我們再次準(zhǔn)備將其用作參數(shù)用于我們的插入查詢.
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;
到你的使用.
這篇關(guān)于C# 將圖片插入 Ms Access的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!