VB.NET Caricamento - Download - File Database MySQL - Percen

di il
1 risposte

VB.NET Caricamento - Download - File Database MySQL - Percen

Salve a tutti.
Per caricare un file in un database MySQL uso questo codice:
 Dim conn As New MySqlConnection       
 Dim cmd As New MySqlCommand


        Dim SQL As String


        Dim FileSize As UInt32
        Dim rawData() As Byte
        Dim fs As FileStream




        conn.ConnectionString = "server=ip;" _
            & "uid=user;" _
            & "pwd=pass;" _
            & "database=test"


        OpenFileDialog1.FileName = ""
        OpenFileDialog1.Filter = "txt files (*.jpg)|*.png|All files (*.*)|*.*"
        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then


            Try


                fs = New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
                FileSize = fs.Length


                rawData = New Byte(FileSize) {}
                fs.Read(rawData, 0, FileSize)
                fs.Close()


                conn.Open()


                SQL = "INSERT INTO upload (file_id,file_name,file_size,file) VALUES(NULL, ?file_name, ?file_size, ?file)"


                cmd.Connection = conn
                cmd.CommandText = SQL
                cmd.Parameters.Add("?file_name", IO.Path.GetFileName(OpenFileDialog1.FileName))
                cmd.Parameters.Add("?file_size", FileSize)
                cmd.Parameters.Add("?file", rawData)


                cmd.ExecuteNonQuery()


                MessageBox.Show("File Inserted into database successfully!", _
                "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)


                conn.Close()
            Catch ex As Exception
                MessageBox.Show("There was an error: " & ex.Message, "Error", _
                    MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try


        End If
Per scaricare un file in un database MySQL uso questo codice:
  Dim conn As New MySqlConnection       
 Dim cmd As New MySqlCommand
        Dim myData As MySqlDataReader
        Dim SQL As String
        Dim rawData() As Byte
        Dim FileSize As UInt32
        Dim fs As FileStream


        conn.ConnectionString = "server=ip;" _
            & "uid=user;" _
            & "pwd=pass;" _
            & "database=test"


        SQL = "SELECT file_name, file_size, file FROM upload where file_id='2';"


        Try
            conn.Open()


            cmd.Connection = conn
            cmd.CommandText = SQL


            myData = cmd.ExecuteReader


            If Not myData.HasRows Then Throw New Exception("There are no BLOBs to save")


            myData.Read()


            FileSize = myData.GetUInt32(myData.GetOrdinal("file_size"))
            rawData = New Byte(FileSize) {}


            myData.GetBytes(myData.GetOrdinal("file"), 0, rawData, 0, FileSize)


            fs = New FileStream("C:\" & myData.GetString(myData.GetOrdinal("file_name")), FileMode.OpenOrCreate, FileAccess.Write)
            fs.Write(rawData, 0, FileSize)
            fs.Close()


            MessageBox.Show("File successfully written to disk!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)


            myData.Close()
            conn.Close()
        Catch ex As Exception
            MessageBox.Show("There was an error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

Vorrei visualizzare la percentuale di caricamento del file in una label ed anche in una progressbar
Come posso fare?
Grazie in anticipo

1 Risposte

Devi accedere o registrarti per scrivere nel forum
1 risposte