[C#]如何将图像插入MS Access数据库? [英] [C#] How will I insert an image to MS Access Database?
本文介绍了[C#]如何将图像插入MS Access数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,在将图像转换为byte []后,如何将图像插入MS Access数据库?
我使用此代码将图像转换为byte []
Hi everyone, how will I insert an image to MS Access database after I converted the image into byte[]?
I used this code to convert the image into byte[]
public byte[] imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] yeah = ms.ToArray() ;
return yeah;
}
提前致谢。 :)
Thanks in advance. :)
推荐答案
像这样:
Like this one:
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();
这是一篇关于它的文章:
使用OLEDB将图像插入MS Access文件 [ ^ ]
public static void AddEmployee(
string lastName,
string firstName,
string title,
DateTime hireDate,
int reportsTo,
string photoFilePath,
string connectionString)
{
byte[] photo = GetPhoto(photoFilePath);
OleDbCommand command = new OleDbCommand(
"INSERT INTO Employees (LastName, FirstName, " +
"Title, HireDate, ReportsTo, Photo) " +
"Values(@LastName, @FirstName, @Title, " +
"@HireDate, @ReportsTo, @Photo)", connection);
command.Parameters.Add("@LastName",
SqlDbType.NVarChar, 20).Value = lastName;
command.Parameters.Add("@FirstName",
SqlDbType.NVarChar, 10).Value = firstName;
command.Parameters.Add("@Title",
SqlDbType.NVarChar, 30).Value = title;
command.Parameters.Add("@HireDate",
SqlDbType.DateTime).Value = hireDate;
command.Parameters.Add("@ReportsTo",
SqlDbType.Int).Value = reportsTo;
command.Parameters.Add("@Photo",
SqlDbType.Image, photo.Length).Value = photo;
connection.Open();
command.ExecuteNonQuery();
}
}
public static byte[] GetPhoto(string filePath)
{
FileStream stream = new FileStream(
filePath, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);
byte[] photo = reader.ReadBytes((int)stream.Length);
reader.Close();
stream.Close();
return photo;
}
来自 MSDN [ ^ ]
请同时参考 -
http://hubpages.com/technology/C-program -that-interface-with-MS-Access-store-data-text-image-in-an-Access-database-file [ ^ ] br />
-KR
From MSDN[^]
Refer this as well-
http://hubpages.com/technology/C-program-that-interfaces-with-MS-Access-Store-data-text-image-in-an-Access-database-file[^]
-KR
这篇关于[C#]如何将图像插入MS Access数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文