Facebook Sharer
选择您要替换的背景颜色:
【农历新年】背景图片:
个性化设定
 注册  找回密码
查看: 1715|回复: 1
打印 上一主题 下一主题

[求助] VB.NET打开MYSQL 文件问题。

[复制链接]

14

主题

2

好友

734

积分

青铜长老

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

跳转到指定楼层
1#
发表于 2012-6-21 02:25 PM |只看该作者 |倒序浏览
各位请帮忙看看,这是我修改一下网上找到的方法。
可是有一些问题...


upload的code
  1. Private Sub cmdUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpload.Click
  2.         lblUploadStatus.Text = ""

  3.         If LTrim(RTrim(txtFilePath.Text)) = "" Then
  4.             lblUploadStatus.Text = "Please select Files!"
  5.             lblUploadStatus.ForeColor = Color.Red
  6.             txtFilePath.Focus()
  7.             Exit Sub
  8.         End If

  9.         Dim sFileToUpload As String = ""
  10.         sFileToUpload = LTrim(RTrim(txtFilePath.Text))
  11.         Dim Extension As String = System.IO.Path.GetExtension(sFileToUpload)
  12.         upLoadImageOrFile(sFileToUpload, Extension)

  13.         isFirst = False
  14.         GetFilesFromDatabase()
  15.     End Sub
  16.     Private Sub upLoadImageOrFile(ByVal sFilePath As String, ByVal sFileType As String)

  17.         Dim builder As New MySqlConnectionStringBuilder
  18.         builder.Server = "localhost"
  19.         builder.UserID = "root"
  20.         builder.Password = "12345"
  21.         builder.Database = "misdatabase"
  22.         Dim str As String = builder.ConnectionString
  23.         Dim conn As New MySqlConnection(str)
  24.         conn.Open()

  25.         Dim SqlCom As MySqlCommand
  26.         Dim imageData As Byte()
  27.         Dim sFileName As String
  28.         Dim qry As String

  29.         Try
  30.             If conn.State = ConnectionState.Closed Then
  31.                 conn.Open()
  32.             End If

  33.             imageData = ReadFile(sFilePath)
  34.             sFileName = System.IO.Path.GetFileName(sFilePath)
  35.             qry = "insert into eFilling (upddate,updUser,fileName,ImageData,fileType,dateUpload,uploadBy) values(@UpdDate,@updUser,@FileName, @ImageData,@FileType,@DateUpload,@UploadBy)"
  36.             SqlCom = New MySqlCommand(qry, conn)
  37.             SqlCom.Parameters.Add(New MySqlParameter("@UpdDate", Now()))
  38.             SqlCom.Parameters.Add(New MySqlParameter("@updUser", frmMain.sEmpID))
  39.             SqlCom.Parameters.Add(New MySqlParameter("@FileName", sFileName))
  40.             SqlCom.Parameters.Add(New MySqlParameter("@ImageData", DirectCast(imageData, Object)))
  41.             SqlCom.Parameters.Add(New MySqlParameter("@FileType", sFileType))
  42.             SqlCom.Parameters.Add(New MySqlParameter("@DateUpload", Now()))
  43.             SqlCom.Parameters.Add(New MySqlParameter("@UploadBy", frmMain.sEmpID))
  44.             SqlCom.ExecuteNonQuery()

  45.             lblUploadStatus.ForeColor = Color.Green
  46.             lblUploadStatus.Text = "File uploaded successfully"
  47.             txtFilePath.Text = ""

  48.         Catch ex As Exception
  49.             MessageBox.Show(ex.ToString())
  50.             lblUploadStatus.Text = "File could not uploaded"
  51.         End Try


  52.     End Sub
复制代码
Database 显示



抓出来的


click了View File Button 的error


打开文件的code

  1.     Private Function ReadFile(ByVal sPath As String) As Byte()
  2.         Dim data As Byte() = Nothing
  3.         Dim fInfo As New FileInfo(sPath)
  4.         Dim numBytes As Long = fInfo.Length
  5.         Dim fStream As New FileStream(sPath, FileMode.Open, FileAccess.Read)
  6.         Dim br As New BinaryReader(fStream)
  7.         data = br.ReadBytes(CInt(numBytes))
  8.         Return data
  9.     End Function

  10.     Private Sub GetFilesFromDatabase()
  11.         Dim builder As New MySqlConnectionStringBuilder
  12.         builder.Server = "localhost"
  13.         builder.UserID = "root"
  14.         builder.Password = "12345"
  15.         builder.Database = "misdatabase"
  16.         Dim str As String = builder.ConnectionString

  17.         Dim conn As New MySqlConnection(str)
  18.         Try
  19.             If conn.State = ConnectionState.Closed Then
  20.                 conn.Open()
  21.             End If

  22.             Dim strSql As String = "Select id,fileName,fileType,dateUpload from eFilling  where uploadby =  '" & frmMain.sEmpID & "'"
  23.             Dim ADAP As New MySqlDataAdapter(strSql, conn)
  24.             Dim DS As New DataSet()

  25.             ADAP.Fill(DS, "eFilling")
  26.             dgdFiles.DataSource = DS.Tables("eFilling")
  27.             Dim dgButtonColumn As New DataGridViewButtonColumn
  28.             dgButtonColumn.HeaderText = "View File"
  29.             dgButtonColumn.UseColumnTextForButtonValue = True
  30.             dgButtonColumn.Text = "View File"
  31.             dgButtonColumn.Name = "ViewFile"
  32.             dgButtonColumn.ToolTipText = "View File"
  33.             dgButtonColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCellsExceptHeader
  34.             dgButtonColumn.FlatStyle = FlatStyle.System
  35.             dgButtonColumn.DefaultCellStyle.BackColor = Color.Gray
  36.             dgButtonColumn.DefaultCellStyle.ForeColor = Color.White
  37.             If isFirst = True Then
  38.                 dgdFiles.Columns.Add(dgButtonColumn)
  39.             End If
  40.         Catch ex As Exception
  41.             MessageBox.Show(ex.ToString())
  42.             MessageBox.Show("Could not load the File")
  43.         End Try
  44.     End Sub

  45.     Private Sub dbGridView_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgdFiles.CellContentClick

  46.         Dim strSql As String = ""


  47.         Try
  48.             Select Case e.ColumnIndex
  49.                 Case Is > -1
  50.                     If sender.Columns(e.ColumnIndex).Name = "ViewFile" Then


  51.                         downLoadFile(dgdFiles.Rows(e.RowIndex).Cells("id").Value, dgdFiles.Rows(e.RowIndex).Cells("FileName").Value, dgdFiles.Rows(e.RowIndex).Cells("FileType").Value)
  52.                     End If
  53.             End Select
  54.         Catch ex As Exception
  55.             MessageBox.Show(ex.ToString())
  56.         End Try

  57.     End Sub

  58.     Private Sub downLoadFile(ByVal iFileId As Long, ByVal sFileName As String, ByVal sFileExtension As String)
  59.         Dim builder As New MySqlConnectionStringBuilder
  60.         builder.Server = "localhost"
  61.         builder.UserID = "root"
  62.         builder.Password = "12345"
  63.         builder.Database = "misdatabase"
  64.         Dim str As String = builder.ConnectionString

  65.         Dim conn As New MySqlConnection(str)
  66.         Dim strSql As String
  67.         Try
  68.             strSql = "Select ImageData from eFilling WHERE id=" & iFileId
  69.             Dim sqlCmd As New MySqlCommand(strSql, conn)
  70.             Dim fileData As Byte() = DirectCast(sqlCmd.ExecuteScalar(), Byte())
  71.             Dim sTempFileName As String = Application.StartupPath & "\" & sFileName

  72.             If Not fileData Is Nothing Then
  73.                 Using fs As New FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Write)
  74.                     fs.Write(fileData, 0, fileData.Length)
  75.                     fs.Flush()
  76.                     fs.Close()
  77.                 End Using
  78.                 Process.Start(sFileName)
  79.             End If

  80.         Catch ex As Exception
  81.             MsgBox(ex.Message)
  82.         End Try

  83.     End Sub

  84. End Class
复制代码
请问是我没有upload到,还是我打开的代码错误呢..各位帮帮忙




收藏收藏0

46

主题

6

好友

6456

积分

百变名嘴

Rank: 13Rank: 13Rank: 13Rank: 13

2#
发表于 2012-6-25 07:46 PM |只看该作者
同鞋,
我看到代码没什么错误。

save 文件进去 db, 是因为你把 file content 放进你的db 了。
正常。


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2024-10-26 08:27 PM , Processed in 0.107895 second(s), 28 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

Ultra High-performance Dedicated Server powered by iCore Technology Sdn. Bhd.
Domain Registration | Web Hosting | Email Hosting | Forum Hosting | ECShop Hosting | Dedicated Server | Colocation Services
本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved
合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部