Can someone help me take a look.

  • Thread starter Thread starter christing
  • Start date Start date
C

christing

Guest
I try develop a code to let user can upload any text file, power point file of picture file to the system. like below picture

1621533.jpg

below is the code.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
strCurrentPath = CurDir()
strCurrentPath = Replace(CurDir, "\bin\Debug", "")
strFullDBPath = strCurrentPath + "\My_Data" + "\Database1.mdb"
cnn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=123456;"

DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

RefreshData()
DataGridView1.Columns("Column2").DefaultCellStyle.ForeColor = Color.Blue
DataGridView1.Columns("Column2").DefaultCellStyle.Font = New Font(" 宋体 ", 8, FontStyle.Underline)
DataGridView1.Columns("Column3").DefaultCellStyle.ForeColor = Color.Blue
DataGridView1.Columns("Column3").DefaultCellStyle.Font = New Font(" 宋体 ", 8, FontStyle.Underline)
End Sub


Sub RefreshData()

Dim constr As String = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=123456;"
Dim con As OleDbConnection = New OleDbConnection(constr)

Dim connection As New OleDbConnection(constr)
Dim sda As New OleDbDataAdapter("select * from FileInfo1", connection)

Dim dt As New DataTable
sda.Fill(dt)
DataGridView1.AutoGenerateColumns = False
DataGridView1.DataSource = dt
DataGridView1.Columns("Column1").DataPropertyName = "filename"
DataGridView1.Columns("Column2").DataPropertyName = "download"
DataGridView1.Columns("Column3").DataPropertyName = "delete"

End Sub

Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
OpenFileDialog1.Title = "Open File"
OpenFileDialog1.Filter = "All files (*.*)|*.*"
OpenFileDialog1.InitialDirectory = "D:\"
If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
filePath = OpenFileDialog1.FileName
fileName = OpenFileDialog1.SafeFileName
UploadFile(filePath, fileName)
RefreshData()
End If
End Sub


i always face an error message on my upload file part can someone pls help me take a look

Sub UploadFile(ByVal filepath As String, ByVal filename As String)
Dim buffer() As Byte
Dim result As Integer
If String.IsNullOrEmpty(filepath) Then
Else
buffer = File.ReadAllBytes(filepath)
If buffer.Length > 100 * 1024 Then
MsgBox("文件过大")
Else
'Dim constr As String = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=123456;"
'Dim connection As New OleDbConnection(constr)

If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If

cmd.Connection = cnn

cmd.CommandText = "INSERT INTO FileInfo1(filename, file, DownLoad, Delete)" &
"VALUES (@filename, @file, @Download, @Delete)"

cmd.Parameters.AddWithValue("@filename", filename)
cmd.Parameters.AddRange({New OleDbParameter("@file", buffer)})
'cnn.Open()
result = cmd.ExecuteNonQuery()
MsgBox(result)
cnn.Close()
End If

End If
End Sub

Sub UploadFile(ByVal filepath As String, ByVal filename As String)
Dim buffer() As Byte
Dim result As Integer
If String.IsNullOrEmpty(filepath) Then
Else
buffer = File.ReadAllBytes(filepath)
If buffer.Length > 100 * 1024 Then
MsgBox("文件过大")
Else
Dim constr As String = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=123456;"
Dim connection As New OleDbConnection(constr)

Dim cmd As New OleDbCommand("INSERT INTO FileInfo1 (filename,file,Download,Delete)" &
"VALUES ('filename', 'file', 'Download', 'Delete')", connection)

cmd.Parameters.AddWithValue("@filename", filename)
cmd.Parameters.AddRange({New OleDbParameter("@file", buffer)})
connection.Open()
result = cmd.ExecuteNonQuery()

MsgBox(result)
connection.Close()
End If

End If
End Sub

thanks a lot

Continue reading...
 
Back
Top