مثال بسيط (الاجراءات المخزنة - اكسس 2010 + فيجوال بيسك 2015)
كود :
Imports System.Data.OleDb
Imports System.IO
Public Class Form1
Dim str As String = "provider=microsoft.ace.oledb.12.0;data source=db.accdb"
Dim con As New OleDbConnection(str)
Dim cm As New OleDbCommand("", con)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
cm.Parameters.Clear()
cm = New OleDbCommand("", con)
cm.CommandText = "aselect"
cm.CommandType = CommandType.StoredProcedure
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim dr As OleDbDataReader = cm.ExecuteReader
If dr.Read Then
tid.Text = dr(0)
tn.Text = dr(1)
Dim b() As Byte = dr.Item(2)
Dim ms As New MemoryStream(b)
apic.Image = Image.FromStream(ms)
End If
dr.Close()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
If tn.Text.Trim.ToLower = "" Then Exit Sub
If foundcount(tn.Text) < 1 Then
cm.Parameters.Clear()
cm = New OleDbCommand("", con)
cm.CommandText = "ainsert"
cm.Parameters.AddWithValue("@tn", tn.Text)
Dim ms As New MemoryStream
apic.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim b() As Byte = ms.ToArray
cm.Parameters.AddWithValue("@pic", b)
cm.CommandType = CommandType.StoredProcedure
If con.State = ConnectionState.Closed Then con.Open()
cm.ExecuteNonQuery()
Else
MsgBox("الاسم محجوز")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim tname As String = InputBox("ادخل الاسم المراد البحث عنه")
If tname.ToString.Trim <> "" Then
tid.Clear()
tn.Clear()
apic.Image = Nothing
cm.Parameters.Clear()
cm = New OleDbCommand("", con)
cm.CommandText = "asearch"
cm.Parameters.AddWithValue("@tn", tname)
cm.CommandType = CommandType.StoredProcedure
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim dr As OleDbDataReader = cm.ExecuteReader
If dr.Read Then
tid.Text = dr(0)
tn.Text = dr(1)
Dim b() As Byte = dr(2)
Dim ms As New MemoryStream(b)
apic.Image = Image.FromStream(ms)
End If
dr.Close()
End If
End Sub
Private Function foundcount(ByVal t As String) As Integer
cm = New OleDbCommand("", con)
cm.CommandText = "foundcount"
cm.Parameters.AddWithValue("@tn", t)
cm.CommandType = CommandType.StoredProcedure
If con.State = ConnectionState.Closed Then
con.Open()
End If
Return cm.ExecuteScalar
End Function
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
cm = New OleDbCommand("", con)
cm.CommandText = "aupdate"
Dim ms As New MemoryStream
apic.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim b() As Byte = ms.ToArray
cm.Parameters.AddWithValue("@pic", b)
cm.Parameters.AddWithValue("@tid", Val(tid.Text))
cm.CommandType = CommandType.StoredProcedure
If con.State = ConnectionState.Closed Then
con.Open()
End If
cm.ExecuteNonQuery()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
cm = New OleDbCommand("", con)
cm.CommandText = "adelete"
cm.Parameters.AddWithValue("@tid", Val(tid.Text))
cm.CommandType = CommandType.StoredProcedure
If con.State = ConnectionState.Closed Then
con.Open()
End If
cm.ExecuteNonQuery()
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim ofd As New OpenFileDialog
If ofd.ShowDialog = DialogResult.OK Then
tn.Text = IO.Path.GetFileNameWithoutExtension(ofd.FileName)
' apic.Image = Image.FromFile(ofd.FileName)
End If
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim ofd As New OpenFileDialog
If ofd.ShowDialog = DialogResult.OK Then
' tn.Text = IO.Path.GetFileNameWithoutExtension(ofd.FileName)
apic.Image = Image.FromFile(ofd.FileName)
End If
End Sub
End Class