使用VB.NET访问数据库 [英] Access Database with VB.NET
问题描述
1。我想从pic插入/更新图片。带有员工ID,员工图片的访问数据库中的框
1. I want to Insert / Update pic from pic. Box in access database with Employee ID,Employee Pic
并再次从数据库中检索以在pic中显示。方框
and again retrieve from database to display in pic. box
in vb.net
in vb.net
上传您的项目 或发送到我的电子邮箱:softwing@yahoo.com
upload your project or send to my email : softwing@yahoo.com
非常感谢您的帮助
Mushtaque Inamdar
Mushtaque Inamdar
Jeddah KSA
Jeddah KSA
+966503009233
+966503009233
推荐答案
你好Mushtaque,
Hi Mushtaque,
>>我想从pic插入/更新pic。带有员工ID,员工图片的访问数据库中的框,并再次从数据库中检索以在图片中显示。框
>>I want to Insert / Update pic from pic. Box in access database with Employee ID,Employee Pic and again retrieve from database to display in pic. box
您使用什么技术?什么是图片。框?它是Windows窗体应用程序中的pictureBox控件吗?
What technology do you use? And what is the pic. Box? it is a pictureBox control within Windows Forms Application?
如果是这样,请参考以下项目:
If so, please refer to the following project:
首先创建一个存储图像的表:
First create a table to store image:
然后VB.NET代码:
Then the VB.NET code:
Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Namespace StrImageAccessDB
Public Partial Class Form1
Inherits Form
Private con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Data\Database1.accdb;")
Private cmd As OleDbCommand
Private da As OleDbDataAdapter
Private dt As New DataTable()
Public Sub New()
InitializeComponent()
End Sub
Private Sub btnOpen_Click(sender As Object, e As EventArgs)
Dim fDialog As New OpenFileDialog()
fDialog.Title = "Select file to be zip"
fDialog.Filter = "JPG Files|*.jpg|JPEG Files|*.jpeg"
If fDialog.ShowDialog() = DialogResult.OK Then
textBox1.Text = fDialog.FileName.ToString()
End If
End Sub
' 1) Insert into access database
Private Sub btnInsert_Click(sender As Object, e As EventArgs)
If textBox1.Text.Trim() = "" Then
MessageBox.Show("Please select image to upload into database")
Return
End If
Dim FileBytes As Byte() = Nothing
Dim fname As String = textBox1.Text.Substring(textBox1.Text.LastIndexOf("\") + 1, textBox1.Text.Length - (textBox1.Text.LastIndexOf("\") + 1))
'First read bytes from image to insert into table
Dim path As String = textBox1.Text
'change your path here
Dim FS As New FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read)
Dim BR As New BinaryReader(FS)
Dim allbytes As Long = New FileInfo(path).Length
FileBytes = BR.ReadBytes(DirectCast(allbytes, Int32))
' close all instances
FS.Close()
FS.Dispose()
BR.Close()
'Insert into access database
con.Open()
cmd = New OleDbCommand("insert into imgupload(ImgName,Img) values (@Im, @Img)", con)
cmd.Parameters.AddWithValue("@Im", fname)
'alter as per your requirement
cmd.Parameters.AddWithValue("@Img", FileBytes)
cmd.ExecuteNonQuery()
con.Close()
MessageBox.Show("Image insert successfully", "Insert Image into Access Database")
textBox1.Text = ""
End Sub
' 2) Retrieve image from database and display in picture box
Private Sub btnRetrieve_Click(sender As Object, e As EventArgs)
'Save Temporarily that image bytes in one location and give that path to picture box
con.Open()
cmd = New OleDbCommand("select * from imgupload where ID=(select max(ID) from imgupload)", con)
da = New OleDbDataAdapter(cmd)
da.Fill(dt)
If dt.Rows.Count > 0 Then
If dt.Rows(0)("Img") <> DBNull.Value Then
pictureBox1.Image = ByteArrayToImage(DirectCast(dt.Rows(0)("Img"), [Byte]()))
End If
End If
con.Close()
End Sub
Private Function ByteArrayToImage(b As Byte()) As Bitmap
Dim ms As New MemoryStream()
Dim pData As Byte() = b
ms.Write(pData, 0, Convert.ToInt32(pData.Length))
Dim bm As New Bitmap(ms, False)
ms.Dispose()
Return bm
End Function
End Class
End Namespace
更多信息请参阅
此链接。
注意:此响应包含对第三方万维网站点的引用。 Microsoft提供此信息是为了方便您。 Microsoft不控制这些网站,也未测试在这些网站上找到的任何软件或信息;因此,
Microsoft不能就其中发现的任何软件或信息的质量,安全性或适用性做出任何陈述。使用互联网上的任何软件都存在固有的危险,微软提醒您在从互联网上检索任何软件之前确保您完全了解风险。
Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
希望这有帮助!
最好的问候,
Stanly
这篇关于使用VB.NET访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!