type data มันผิดอ่าครับ
จะเอา string ไปใส่เลยไม่ได้ครับ ต้องแปลงเป็น type เดียวกับ data ก่อน
ถ้าเป็นพวกไฟล์ต่างๆ ผมชอบใช้เป็น text(ใน data) มันทำงานง่ายดีครับไม่ต้องแปลงกลับไปกลับมา
Public Class frmAdd
Dim objConn As New MySqlConnection
Dim objCmd As New MySqlCommand
Dim Reader As MySqlDataReader
Private Function ImageToStream(ByVal fileName As String) As Byte()
Dim stream As New MemoryStream()
tryagain:
Try
Dim image As New Bitmap(fileName)
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
Catch ex As Exception
GoTo tryagain
End Try
Return Stream.ToArray()
End Function
Private Sub Browse1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Browse1.Click
Dim fileOpener As OpenFileDialog = New OpenFileDialog()
fileOpener.Filter = "Image files | *.jpg"
If fileOpener.ShowDialog() = Windows.Forms.DialogResult.OK Then
PictureBox1.Image = Image.FromFile(fileOpener.FileName)
TextBox1.Text = fileOpener.FileName
End If
End Sub
Private Sub btnAdd_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim strConnString, strSQL As String
Dim FileName As String = TextBox1.Text
Dim content As Byte() = ImageToStream(FileName)
Try
strConnString = "Server=localhost;User Id=root; Password=0000; Database=members; Pooling=false"
objConn.ConnectionString = strConnString
objConn.Open()
strSQL = "insert into members.member (CustomerID,Username,Password,Name,Email,Age,Gade,Wicha,Class,Idcard,Rg,Nick,Adress,Pic) values ('" & textbox_cid.Text & "','" & textbox_username.Text & "','" & textbox_password.Text & "','" & textbox_name.Text & "','" & textbox_email.Text & "','" & textbox_age.Text & "','" & textbox_gade.Text & "','" & textbox_wicha.Text & "','" & textbox_class.Text & "','" & textbox_idcard.Text & "','" & textbox_rg.Text & "','" & textbox_nick.Text & "','" & RichTextBox1.Text & "',@FileName)"
objCmd = New MySqlCommand(strSQL, objConn)
objCmd.Parameters.Add("@Pic", content)
objCmd.ExecuteNonQuery()
MessageBox.Show("บันทึกข้อมูลเรียบร้อย!")
objConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
objConn.Dispose()
End Try
End Sub
Private Function ImageToStream(ByVal fileName As String) As Byte()
Dim stream As New MemoryStream()
Dim image As Image = Image.FromFile(fileName)
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
MessageBox.Show(Stream.ToArray().Length.ToString())
Return Stream.ToArray()
End Function
Private Sub Browse1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Browse1.Click
Dim fileOpener As OpenFileDialog = New OpenFileDialog()
fileOpener.Filter = "Image files | *.jpg"
If fileOpener.ShowDialog() = Windows.Forms.DialogResult.OK Then
PictureBox1.Image = Image.FromFile(fileOpener.FileName)
TextBox1.Text = fileOpener.FileName
End If
End Sub
Private Sub btnAdd_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim strConnString, strSQL As String
Dim FileName As String = TextBox1.Text
Dim Image As Byte() = ImageToStream(FileName)
Try
strConnString = "Server=localhost;User Id=root; Password=0000; Database=members; Pooling=false"
objConn.ConnectionString = strConnString
objConn.Open()
strSQL = "insert into members.member (CustomerID,Username,Password,Name,Email,Age,Gade,Wicha,Class,Idcard,Rg,Nick,Adress,Pic) values ('" & textbox_cid.Text & "','" & textbox_username.Text & "','" & textbox_password.Text & "','" & textbox_name.Text & "','" & textbox_email.Text & "','" & textbox_age.Text & "','" & textbox_gade.Text & "','" & textbox_wicha.Text & "','" & textbox_class.Text & "','" & textbox_idcard.Text & "','" & textbox_rg.Text & "','" & textbox_nick.Text & "','" & RichTextBox1.Text & "', ' Image ')"
objCmd = New MySqlCommand(strSQL, objConn)
objCmd.Parameters.Add("@Image", MySqlDbType.Blob)
Reader = objCmd.ExecuteReader
MessageBox.Show("บันทึกข้อมูลเรียบร้อย!")
objConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
objConn.Dispose()
End Try
End Sub
Code (VB.NET)
Public Shared Function ByteArrayToImage(ByVal byteArrayIn As Byte()) As Image
Dim ms = New System.IO.MemoryStream(byteArrayIn)
Dim returnImage As Image = Image.FromStream(ms)
Return returnImage
End Function
Public Sub LoadInfor()
Dim objConn As New MySqlConnection
Dim objCmd As New MySqlCommand
Dim dtAdapter As MySqlDataAdapter
Dim dt As New DataTable
Dim strConnString, strSQL As String
strConnString = "Server=localhost;User Id=root; Password=0000; Database=members; Pooling=false"
objConn.ConnectionString = strConnString
objConn.Open()
strSQL = "SELECT * FROM member WHERE Username = '" & strUser & "' "
dtAdapter = New MySqlDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
If dt.Rows.Count > 0 Then
Me.lblUser.Text = strUser
Me.lblName.Text = dt.Rows(0)("Name")
Me.lblEmail.Text = dt.Rows(0)("Email")
Me.lblAge.Text = dt.Rows(0)("Age")
Me.lblID.Text = dt.Rows(0)("CustomerID")
Me.lblGade.Text = dt.Rows(0)("Gade")
Me.lblWicha.Text = dt.Rows(0)("Wicha")
Me.lblClass.Text = dt.Rows(0)("Class")
Me.lblIdcard.Text = dt.Rows(0)("Idcard")
Me.lblRg.Text = dt.Rows(0)("Rg")
Me.lblNick.Text = dt.Rows(0)("Nick")
PictureBox3.Image = ByteArrayToImage(dt.Rows(0)("Pic"))
Me.RichTextBox1.Text = dt.Rows(0)("Adress")
End If
objConn.Close()
objConn = Nothing
End Sub