none
VB2005保存图片到数据库出错 RRS feed

  • 问题

  •  

    出错原因提示:必须声明变量‘@photo’

     

    代码:

     Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click

     

                Dim sql As String
                Dim sqlcomm As New SqlClient.SqlCommand
                Dim data(fs.Length) As Byte
                fs.Read(data, 0, Int(fs.Length))
                Dim prm3 As New SqlClient.SqlParameter("@photo", SqlDbType.VarBinary, Int(fs.Length), ParameterDirection.Input, False, 0, 0, "", DataRowVersion.Current, data)
                sqlcomm.Parameters.Add(prm3)

    ``````以上代码是在网上复制的,目的在将图片进行2进制转换

               

                sql = "insert into ygdab(ygid,ygmc,xbid,xlid,jrsj,bmid,zwid,zznx,sfzh,sfzzid,ygtx) values( '" & TextBox13.Text & "','" & TextBox2.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & Me.UltraDateTimeEditor2.Value & "','" & TextBox9.Text & "','" & TextBox10.Text & "','" & TextBox3.Text & "','" & TextBox11.Text & "',@photo)"

                    exec1(sql)
                    MsgBox("增加成功", MsgBoxStyle.Information, "提示")
                    Exit Sub
           End Sub

    请高手帮我看看该怎么修改和加入这个声明变量

     

    2008年8月24日 11:12

答案

全部回复

  • 'exec1(sql)

    sqlcomm.executenoquery(sql)

    2008年8月25日 1:00
    版主
  • 按你的意思就是这样

     

     sql = "insert into ygdab(ygid,ygmc,xbid,xlid,jrsj,bmid,zwid,zznx,sfzh,sfzzid,ygtx) values( '" & TextBox13.Text & "','" & TextBox2.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & Me.UltraDateTimeEditor2.Value & "','" & TextBox9.Text & "','" & TextBox10.Text & "','" & TextBox3.Text & "','" & TextBox11.Text & "',@photo)"

     sqlcomm.ExecuteNonQuery(sql)

    msgbox("添加成功")

    exit sub

     

    错误 1 没有可访问的“ExecuteNonQuery”接受此数目的参数,因此重载决策失败。

     

     

     

    我的EXEC1是在模块里面就声明的

     

    模块代码:

         Public Sub exec1(ByVal sql As String)
            Try
                Dim sqlcmd As New SqlCommand
                conn.Open()
                sqlcmd.CommandText = sql
                sqlcmd.Connection = conn
                sqlcmd.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox(ex.ToString, MsgBoxStyle.Information, "try")
            Finally
                conn.Close()

            End Try
        End Sub

     

     

    保存图片代码:

     Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
                    
                Dim sql As String
                Dim mrc As DataTable

                Dim sqlcomm As New SqlClient.SqlCommand
                Dim data(fs.Length) As Byte
                fs.Read(data, 0, Int(fs.Length))
                Dim prm3 As New SqlClient.SqlParameter("@photo", SqlDbType.VarBinary, Int(fs.Length), ParameterDirection.Input, False, 0, 0, "", DataRowVersion.Current, data)
                sqlcomm.Parameters.Add(prm3)

               
                    
        sql = "insert into ygdab(ygid,ygmc,xbid,xlid,jrsj,bmid,zwid,zznx,sfzh,sfzzid,ygtx) values( '" & TextBox13.Text & "','" & TextBox2.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & Me.UltraDateTimeEditor2.Value & "','" & TextBox9.Text & "','" & TextBox10.Text & "','" & TextBox3.Text & "','" & TextBox11.Text & "',@photo)"
            exec1(sql)
                    MsgBox("增加成功", MsgBoxStyle.Information, "提示")
                    Exit Sub
                                            End Sub

     

    所以请版主帮我看一下我该怎么改????

     

    2008年8月25日 5:57
  • Public Sub exec1(ByVal sql As String, prm3 As SqlClient.SqlParameter)
            Try
                Dim sqlcmd As New SqlCommand
                conn.Open()
                sqlcmd.CommandText = sql
                sqlcmd.Connection = conn

    sqlcomm.Parameters.Add(prm3)
                sqlcmd.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox(ex.ToString, MsgBoxStyle.Information, "try")
            Finally
                conn.Close()

            End Try
        End Sub

    2008年8月25日 6:15
    版主
  • 版主的代码放上去有2个错误

          错误 1 没有为“Public Sub exec3(sql As String, prm3 As System.Data.SqlClient.SqlParameter)”的参数“prm3”指定参数。

          错误 2  fs 应为声明。

     

    按版主的意图我修改为:

     

    Module Module1'''模块代码
        Private fs As IO.FileStream
        Dim conn As SqlConnection = New SqlConnection("server=(local);database=csgl;uid=sa;pwd=111111")
        Dim data(fs.Length) As Byte
        fs.Read(data, 0, Int(fs.Length))'''''fs 应为声明
        Dim prm3 As New SqlClient.SqlParameter("@photo", SqlDbType.VarBinary, Int(fs.Length),ParameterDirection.Input,     False, 0, 0, "", DataRowVersion.Current, data)

       Public Sub exec3(ByVal sql As String, ByVal prm3 As SqlClient.SqlParameter)
            Try
                Dim sqlcmd As New SqlCommand
                conn.Open()
                sqlcmd.CommandText = sql
                sqlcmd.Connection = conn
                sqlcmd.Parameters.Add(prm3)
                sqlcmd.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox(ex.ToString, MsgBoxStyle.Information, "try")
            Finally
                conn.Close()

            End Try
        End Sub

    End Module

     

    窗体Form1中保存图片的代码

     Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click

     

                Dim sql As String
                Dim mrc As DataTable

                

    sql = "insert into ygdab(ygid,ygmc,xbid,xlid,jrsj,bmid,zwid,zznx,sfzh,sfzzid,ygtx) values( '" & TextBox13.Text & "','" & TextBox2.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & Me.UltraDateTimeEditor2.Value & "','" & TextBox9.Text & "','" & TextBox10.Text & "','" & TextBox3.Text & "','" & TextBox11.Text & "','@photo')"
               

      exec3(sql)'''错误 1 没有为“Public Sub exec3(sql As String, prm3 As System.Data.SqlClient.SqlParameter)”的参数“prm3”指定参数
                

     MsgBox("增加成功", MsgBoxStyle.Information, "提示")
                    Exit Sub

    2008年8月25日 10:31
  • 你定义了参数又不传?

     

     

      Private fs As IO New FileStream(图片文件名, FileMode.Open)
          Dim data(fs.Length) As Byte
        fs.Read(data, 0, Int(fs.Length))
        Dim prm3 As New SqlClient.SqlParameter("@photo", SqlDbType.VarBinary, Int(fs.Length),ParameterDirection.Input,     False, 0, 0, "", DataRowVersion.Current, data)

    exec3(sql,prm3)
    2008年8月26日 1:01
    版主
  • 错误 5 数组界限不能出现在类型说明符中。

    模块代码

    Module Module1
        Private fs As IO.FileStream("photo",FileMode.Open)'''数组界限不能出现在类型说明符中
        Dim conn As SqlConnection = New SqlConnection("server=(local);database=csgl;uid=sa;pwd=111111")
        Dim data(fs.Length) As Byte
        fs.Read(data, 0, Int(fs.Length))
        Dim prm3 As New SqlClient.SqlParameter("@photo", SqlDbType.VarBinary, Int(fs.Length), ParameterDirection.Input, False, 0, 0, "", DataRowVersion.Current, data)

     

     Public Sub exec3(ByVal sql As String, ByVal prm3 As SqlClient.SqlParameter)
            Try
                Dim sqlcmd As New SqlCommand
                conn.Open()
                sqlcmd.CommandText = sql
                sqlcmd.Connection = conn
                sqlcmd.Parameters.Add(prm3)
                sqlcmd.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox(ex.ToString, MsgBoxStyle.Information, "try")
            Finally
                conn.Close()

            End Try
        End Sub

     

    end module

     

     

    FORM窗体保存图片代码

     

     Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
            

    dim sql as string

     

      sql = "insert into ygdab(ygid,ygmc,xbid,xlid,jrsj,bmid,zwid,zznx,sfzh,sfzzid,ygtx) values( '" & TextBox13.Text & "','" & TextBox2.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & Me.UltraDateTimeEditor2.Value & "','" & TextBox9.Text & "','" & TextBox10.Text & "','" & TextBox3.Text & "','" & TextBox11.Text & "','@photo')"
                    exec3(sql, prm3)'''未声明名称prm3     我只在模块里定义了一下
                    MsgBox("增加成功", MsgBoxStyle.Information, "提示")
                    Exit Sub

     

        End Sub

     

    请版主能不能给我远程协助一下,这2个问题已经搞了一个星期了..我还没搞定

    我的 QQ是:280293896

    请你在百忙之中抽出点时间帮我远程一下..十分感激

     

     

    2008年8月26日 4:53
  • prm3定义在form

    2008年8月26日 5:44
    版主
  • Module Module1
        Private fs As IO.FileStream("photo",FileMode.Open)'''数组界限不能出现在类型说明符中
        Dim conn As SqlConnection = New SqlConnection("server=(local);database=csgl;uid=sa;pwd=111111")
        Dim data(fs.Length) As Byte
        fs.Read(data, 0, Int(fs.Length))
        Dim prm3 As New SqlClient.SqlParameter("@photo", SqlDbType.VarBinary, Int(fs.Length), ParameterDirection.Input, False, 0, 0, "", DataRowVersion.Current, data)

     

     Public Sub exec3(ByVal sql As String, ByVal prm3 As SqlClient.SqlParameter)
            Try
                Dim sqlcmd As New SqlCommand
                conn.Open()
                sqlcmd.CommandText = sql
                sqlcmd.Connection = conn
                sqlcmd.Parameters.Add(prm3)
                sqlcmd.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox(ex.ToString, MsgBoxStyle.Information, "try")
            Finally
                conn.Close()

            End Try
        End Sub

     

    end module

     

     

    FORM窗体保存图片代码

     

     Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
            

    dim sql as string

     Private fs As IO.FileStream
        Dim conn As SqlConnection = New SqlConnection("server=(local);database=csgl;uid=sa;pwd=111111")
        Dim data(fs.Length) As Byte
        fs.Read(data, 0, Int(fs.Length))
        Dim prm3 As New SqlClient.SqlParameter("@photo", SqlDbType.VarBinary, Int(fs.Length), ParameterDirection.Input, False, 0, 0, "", DataRowVersion.Current, data)

     

     

      sql = "insert into ygdab(ygid,ygmc,xbid,xlid,jrsj,bmid,zwid,zznx,sfzh,sfzzid,ygtx) values( '" & TextBox13.Text & "','" & TextBox2.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & Me.UltraDateTimeEditor2.Value & "','" & TextBox9.Text & "','" & TextBox10.Text & "','" & TextBox3.Text & "','" & TextBox11.Text & "','@photo')"
                    exec3(sql, prm3)                MsgBox("增加成功", MsgBoxStyle.Information, "提示")
                    Exit Sub

     

        End Sub

     

     

    Private fs As IO.FileStream("photo",FileMode.Open)'''数组界限不能出现在类型说明符中

    请版主帮我在看看这是怎么回事?


     

    2008年8月26日 6:09
  •  

    谢谢版主了....已经搞好了
    2008年8月26日 7:40