كود :
Imports System.Data.SqlClient
Imports System.IO
Public Class Form1
Dim Constr As New SqlConnection("Server=Hafedh;Database=TESTDB;Integrated Security=false;user id=sa;password=**********;")
Dim dds As DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dds = New DataTable
Using InfoAdapter As New SqlDataAdapter("select * from Customers", Constr)
InfoAdapter.Fill(dds)
End Using
DataGridView1.DataSource = dds
TextBox1.DataBindings.Add("Text", dds, "Idnum")
TextBox2.DataBindings.Add("Text", dds, "NameCust")
TextBox3.DataBindings.Add("Text", dds, "Oldyear")
TextBox4.DataBindings.Add("Text", dds, "PhoneCust")
TextBox5.DataBindings.Add("Text", dds, "AdressCust")
PictureBox1.DataBindings.Add("Image", dds, "PictureFile", True, Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)
'PictureBox1.DataBindings.Add("Text", InfoTable, "PictureFile")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
If TextBox2.Text = "" Then
MsgBox("Please fill in the data first")
Exit Sub
End If
Dim opn As New OpenFileDialog
opn.Title = "Select a photo "
opn.Filter = "Images|*.jpg;*.jpeg;*.png;*.gif"
opn.Multiselect = False
If opn.ShowDialog() = DialogResult.OK Then
PictureBox1.Image = Image.FromFile(opn.FileName)
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error loading image", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using Command As New SqlCommand With {.Connection = Constr}
With Command
'إضافة الصورة
Dim ms As New MemoryStream
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
.CommandText = "INSERT INTO Customers(Idnum,NameCust,Oldyear,PhoneCust,AdressCust,PictureFile)values(@Idnum,@NameCust,@Oldyear,@PhoneCust,@AdressCust,@PictureFile)"
.Parameters.Add("@Idnum", SqlDbType.Decimal, 18).Value = Decimal.Parse(TextBox1.Text)
.Parameters.Add("@NameCust", SqlDbType.NVarChar).Value = TextBox2.Text
.Parameters.Add("@Oldyear", SqlDbType.NVarChar).Value = TextBox3.Text
.Parameters.Add("@PhoneCust", SqlDbType.NVarChar).Value = TextBox4.Text
.Parameters.Add("@AdressCust", SqlDbType.NVarChar).Value = TextBox5.Text
.Parameters.Add("@PictureFile", SqlDbType.Image).Value = ms.ToArray()
End With
If Constr.State = ConnectionState.Closed Then Constr.Open()
If Command.ExecuteNonQuery() = 1 Then
MsgBox("Success record", MsgBoxStyle.MsgBoxRtlReading, "ADD Record")
Else
MsgBox("Make sure all data is correctly entered", MsgBoxStyle.MsgBoxRtlReading, "Error")
End If
Constr.Close()
End Using
End Sub
End Class