从mysql数据库VB.NET下载后我无法打开文件 [英] I can't open document file after downloading from mysql database VB.NET

查看:92
本文介绍了从mysql数据库VB.NET下载后我无法打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Private Sub DownloadFile(NewFileLocation As String)
       Try
           NewFileLocation = DirPathName
           Dim id As Integer = Convert.ToInt32(gvFile.CurrentRow.Cells(0).Value.ToString())
           query = "Select * From tbl_login Where Id=@Id"
           cmd = New MySqlCommand(query, conn)
           cmd.Parameters.AddWithValue("@Id", id)
           da = New MySqlDataAdapter(cmd)
           dt = New DataTable("tbl_login")
           dt.Clear()
           da.Fill(dt)

           For Each row As DataRow In dt.Rows
               Dim filebyte As Byte() = row(2)
               Dim fs As New FileStream(NewFileLocation & "\" & row(1), FileMode.Create, FileAccess.Write)
               fs.Write(filebyte, 0, filebyte.Length)
               fs.Close()
           Next
           txtDownloadPath.Clear()
           MessageBox.Show("File Downloaded Successfully", "Download", MessageBoxButtons.OK, MessageBoxIcon.Information)
       Catch ex As Exception
       End Try
   End Sub





我尝试过:



从数据库下载后无法打开任何类型的文件,代码有什么问题?谢谢!



What I have tried:

any type of file can't open after downloading from database, what's wrong in code? Thanks!

推荐答案

要添加到CHill60所说的内容,请不要吞下异常!

当你编写这样的代码时:

To add to what CHill60 said, don't swallow exceptions!
When you write code like this:
Try
   ...
Catch ex As Exception
End Try

这将是一个问题。



首先,当它失败时,你不知道 - 因为你吞下了异常,用户永远不会被告知有问题所以不告诉你。

其次,即使你知道这段代码不能正常工作,也很可能是它提出了一个例外 - 但你故意丢弃了所有可能告诉你问题是什么以及它为什么会发生的信息!

一个空的挡块说我不知道我是什么我在做:我不想要错误所以我会忽略他们都是。

这是你未来自己的PITA:他没有得到任何东西告诉他问题可能在哪里或什么。


因此,在每个catch块中,记录异常信息以便稍后进行研究,或者至少向用户显示存在问题以及它是什么!如果他的下载工作正常,他会非常恼火你,但是你的代码因为错误而抛弃它而没有告诉他!

It's going to be a problem.

The first thing is that when it fails, you don't know - because you have swallowed the exception, the user is never told there was a problem and so doesn't tell you.
Secondly, even if you know this code doesn't work properly, the chances are that it threw an exception - but you have deliberately discarded all the information that might have told you what the problem was and why it happened!
An empty catch block is saying "I don't know what I'm doing: I don't want errors so I'll just ignore 'em all."
And that's a PITA for your future self: he doesn't get anything to tell him where or what the problem might be.

So inside every catch block, log the exception info so you can study it later, or at the very least show the user there was a problem and what it was! He's going to be very annoyed with you if his download worked fine, but your code threw it away on an error and didn't tell him!


这篇关于从mysql数据库VB.NET下载后我无法打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆