我試圖轉換圖像插入到C#.Net Windows窗體中的Microsoft Access數據庫。 而且我想刪除和改變圖像。 但我不知道該怎麼做。 請爲我寫一段代碼。如何將圖像插入到Microsoft Access數據庫?
1
A
回答
3
我會將圖像轉換爲一個字節[]和。然後將字節[]轉換回圖像。
public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
public static Image ImageFromByte(byte[] image)
{
ImageConverter ic = new ImageConverter();
Image img = (Image)ic.ConvertFrom(image);
return img;
}
0
下面的鏈接將幫助您:
轉換將在上面的鏈接的下面幾行:
using (FileStream fs = fi.OpenRead())
{
bData = new byte[fi.Length];
int nReadLength = fs.Read(bData,0, (int)(fi.Length));
}
希望這有助於...
0
using (var connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\BlankDatabase.mdb"))
{
connection.Open();
// Create table
using (var command = connection.CreateCommand())
{
command.CommandText = @"
CREATE TABLE FileTable (
FileName VARCHAR(255),
File IMAGE)
";
command.ExecuteNonQuery();
}
var imageContent = File.ReadAllBytes(@"C:\logo.png");
// upload image to the table
using (var command = connection.CreateCommand())
{
command.CommandText = @"
INSERT INTO FileTable (FileName, File)
VALUES (@FileName, @File)
";
command.Parameters.AddWithValue("@FileName", "Logo");
command.Parameters.AddWithValue("@File", imageContent);
command.ExecuteNonQuery();
}
// retreive image from the table
using (var command = connection.CreateCommand())
{
command.CommandText = @"
SELECT File
FROM FileTable
WHERE FileName = 'Logo'
";
var readImageContent = (byte[])command.ExecuteScalar();
File.WriteAllBytes(@"C:\logo1.png", readImageContent);
}
// alter image from the table
using (var command = connection.CreateCommand())
{
command.CommandText = @"
UPDATE FileTable
SET File = @File
WHERE FileName = 'Logo'
";
command.Parameters.AddWithValue("@File", imageContent);
command.ExecuteNonQuery();
}
// delete image from the table
using (var command = connection.CreateCommand())
{
command.CommandText = @"
DELETE FROM FileTable
WHERE FileName = 'Logo'
";
command.ExecuteNonQuery();
}
}
在此代碼中BlankDatabase.mdb
是一個空的MS Access數據庫文件。
相關問題
- 1. 如何將數據插入Microsoft Access數據庫?
- 2. 在VB.Net中插入Microsoft Access數據庫
- 3. 將PDF數據插入Microsoft Access表
- 4. 如何將圖像插入數據庫?
- 5. 如何將圖像插入數據庫?
- 6. 插入到Access數據庫
- 7. 如何通過ado代碼將值插入到Microsoft Access數據庫中?
- 8. 插入數據到MS Access數據庫
- 9. 插入圖像到MS Access數據庫使用C#
- 10. 如何將Microsoft Access數據庫保存爲.sqlite數據庫?
- 11. C#:無法在我的Microsoft Access數據庫中插入數據
- 12. 將圖像從PHP插入到Access
- 13. 如何將圖像插入到mysql數據庫中
- 14. 如何將圖像插入到mysql數據庫?
- 15. 如何將圖像插入到sql server數據庫?
- 16. 如何將圖像插入到sqlite數據庫?
- 17. 如何將圖像插入到SQL Server數據庫表
- 18. 如何從數據庫插入圖像?
- 19. 如何將SQLite數據庫轉換/導出到Microsoft Access?
- 20. 如何將Microsoft Access數據庫連接到可視化c#?
- 21. 將記錄插入Access數據庫
- 22. 將圖像插入數據庫中
- 23. 無法將圖像插入數據庫
- 24. 將圖像目錄插入數據庫
- 25. 將圖像插入數據庫
- 26. PHP - 將圖像插入數據庫
- 27. C#插入Access數據庫
- 28. 用於使用Java將圖像插入到MS Access數據庫的SQL語句
- 29. 如何使用Microsoft Access將舊數據庫遷移到新數據庫中
- 30. 將Microsoft Access數據庫上載到Microsoft SQL Server Express數據庫中
請幫幫我。我需要你的幫助。 – Saya