在数据库中上传文件两次

时间:2011-12-14 20:02:40

标签: asp.net sql

我使用ASP.net程序和SQL数据库

我想将各种文件上传到数据库

我使用此代码执行此操作,但它确实在数据库中上传了两次文件

Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Partial Class Document
    Inherits System.Web.UI.Page

    Public Function InsertUpdateData(ByVal cmd As SqlCommand) As Boolean
        'Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("conString").ConnectionString
        Dim con As New SqlConnection("Data Source=SON\SQLDB;Initial Catalog=myDB;Integrated Security=True")
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        Try
            con.Open()
            cmd.ExecuteNonQuery()
            Return True
        Catch ex As Exception
            Response.Write(ex.Message)
            Return False
        Finally
            con.Close()
            con.Dispose()
        End Try
    End Function

    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        ' Read the file and convert it to Byte Array 
        Dim filePath As String = FileUpload1.PostedFile.FileName
        Dim filename As String = Path.GetFileName(filePath)
        Dim ext As String = Path.GetExtension(filename)
        Dim contenttype As String = String.Empty

        'Set the contenttype based on File Extension
        Select Case ext
            Case ".doc"
                contenttype = "application/vnd.ms-word"
                Exit Select
            Case ".docx"
                contenttype = "application/vnd.ms-word"
                Exit Select
            Case ".xls"
                contenttype = "application/vnd.ms-excel"
                Exit Select
            Case ".xlsx"
                contenttype = "application/vnd.ms-excel"
                Exit Select
            Case ".jpg"
                contenttype = "image/jpg"
                Exit Select
            Case ".png"
                contenttype = "image/png"
                Exit Select
            Case ".gif"
                contenttype = "image/gif"
                Exit Select
            Case ".pdf"
                contenttype = "application/pdf"
                Exit Select
        End Select
        If contenttype <> String.Empty Then
            Dim fs As Stream = FileUpload1.PostedFile.InputStream
            Dim br As New BinaryReader(fs)
            Dim bytes As Byte() = br.ReadBytes(fs.Length)

            'insert the file into database 
            Dim strQuery As String = "insert into ArchivedFile" _
            & "(Name, contenttype, Data )" _
            & " values (@Name, @ContentType, @Data)"
            Dim cmd As New SqlCommand(strQuery)
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename
            cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value _
            = contenttype
            cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes
            InsertUpdateData(cmd)
            lblMessage.ForeColor = System.Drawing.Color.Green
            lblMessage.Text = "File Uploaded Successfully"
        Else
            lblMessage.ForeColor = System.Drawing.Color.Red
            lblMessage.Text = "File format not recognised." _
            & " Upload Image/Word/PDF/Excel formats"
        End If
    End Sub
End Class 

我不知道确切的问题是什么 我希望有人可以帮助我。

2 个答案:

答案 0 :(得分:2)

你有一个按钮附加的代码 - 有人只是双击按钮吗?

答案 1 :(得分:0)

我要做的第一件事是确定代码的哪些部分正在运行两次。使用调试和断点来确定Sub btnUpload_Click运行的次数,调用Function InsertUpdateData的次数等。

另外,从你的问题的上下文我收集双重插入是在你测试时发生,而不仅仅是在生产中。那是准确的吗?

如果这不能直接导致您解决问题,请使用结果更新您的问题,以便我们进一步检查。

我怀疑btnUpload_Click由于某种原因会两次触发,如果情况确实如此,请发布按钮的标记,是否包含在UpdatePanel中,以及任何客户端脚本这可能适用。