[VB.Net]如何将图像数据导入另一个数据库 [英] [VB.Net] How to get data of image into another database

查看:77
本文介绍了[VB.Net]如何将图像数据导入另一个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在列表视图中有条形码数据,该数据是由条形码的For Loop下载的,然后查询条形码的数据以将发送的图像插入到另一个数据库中. 


我有代码,但结果就像第1行上的插入正确,但第2行和第3行与条形码无关.


I have code but the result comes like insert on row 1 was correct but row2 and 3 weren't related to barcode.

Dim shw As String = TextBox1.Text
        Dim date2 As String = DateTimePicker1.Text
        Dim namec As String = TextBox3.Text
        Dim objConn As New OleDbConnection
        Dim objCmd As New OleDbCommand
        Dim strConnString As String
        Dim cmdUpdate As New OleDbCommand
        Dim img2 As Byte()
        olecon = New OleDbConnection
        olecon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\database\database2.accdb;"
        olecon.Open()
        strConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\database\databaseorder.mdb;"
        objConn.ConnectionString = strConnString

        objConn.Open()
        Dim i As Integer = ListView1.Items.Count - 1
        For j As Integer = 0 To i

            Dim num As String = ListView1.Items(j).SubItems(0).Text
            Dim barp As String = ListView1.Items(j).SubItems(1).Text
            Dim namep As String = ListView1.Items(j).SubItems(2).Text
            Dim pricep As Double = ListView1.Items(j).SubItems(3).Text
            Dim coup As Double = ListView1.Items(j).SubItems(4).Text
            Dim sump As Double = ListView1.Items(j).SubItems(5).Text
            oledb = "select * from warehouse where barcode = '" & barp & "'"
            Dim dts2 As DataTable = cmd_excuteToDataTable()
            MsgBox(oledb)
            img2 = dts2.Rows(0)("picofpro")





            cmdUpdate.CommandText = "INSERT INTO ordercus (order_code,no_id,barcode,namecus,namepro,pricepro,countpro,sumprice,dateorder,pic_pro) VALUES ('" & shw & "','" & num & "','" & barp & "','" & namec & "','" & namep & "','" & pricep & "','" & coup & "','" & sump & "','" & date2 & "',@img);"
            cmdUpdate.Parameters.Add("@img", OleDbType.LongVarBinary).Value = img2
            cmdUpdate.CommandType = CommandType.Text

            cmdUpdate.Connection = objConn
            cmdUpdate.ExecuteNonQuery()


如果有人有想法,请帮助
非常感谢

If anyone got an idea, Please help
Thank you so much

推荐答案

你好

我看到了几件事,首先是在每次迭代中添加一个参数,该参数是迭代的,因此在两个循环中,该参数在其中两次存在并且应该是一次.您应该按如下所示设置参数,主要原因之一是字符串值包含 撇号,并且(在该项目中可能不必担心)打开代码进行SQL注入.

Several things I see, first you are adding a parameter on each iteration, that is iterative so in two loops the parameter is in there twice and should be once. You should setup parameters as shown below, one of the main reasons are strings values containing apostrophes and (this may not be a concern in this project) opens code up to SQL injection.

下一步,您不检查ExecuteNonQuery的结果.

Next, you are not checking the result from ExecuteNonQuery.

我建议您使用逻辑,在该逻辑中,您可以对每个值进行参数化,依次进行for/each或for/next并运行插入操作.每次获取新的主键.如果下面的代码中存在问题,该函数将返回false并设置异常 造成财产损失的原因.

What I would recommend is using logic where you parameterize each value, go through a for/each or for/next and run the insert.  Each time get the new primary key. If there is an issue in the code below the function returns false and sets the exception for the failure into a property.

您不需要将代码放入类中,我这样做是很容易阅读的,建议您使用类.

You don't need to place your code into a class, I did so it's easy to read and would suggest using a class.

Public Class Sample4
    Private mExceptiom As Exception
    Public ReadOnly Property Exception As Exception
        Get
            Return mExceptiom
        End Get
    End Property

    Private Builder As New OleDbConnectionStringBuilder With
    {
        .Provider = "Microsoft.ACE.OLEDB.12.0",
        .DataSource = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database1.accdb")
    }
    Public Function AddNewRow(ByVal PersonList As List(Of Person)) As Boolean
        Dim Success As Boolean = True
        Dim Affected As Integer = 0


        Try
            Using cn As New OleDbConnection With {.ConnectionString = Builder.ConnectionString}
                Using cmd As New OleDbCommand With {.Connection = cn}

                    cmd.CommandText = "INSERT INTO Customer (CompanyName,ContactName,Image) Values(@CompanyName,@ContactName,@Image)"

                    cmd.Parameters.Add(New OleDbParameter With {.ParameterName = "@CompanyName", .DbType = DbType.String})
                    cmd.Parameters.Add(New OleDbParameter With {.ParameterName = "@ContactName", .DbType = DbType.String})
                    cmd.Parameters.Add(New OleDbParameter With {.ParameterName = "@Image", .OleDbType = OleDbType.LongVarBinary})

                    cn.Open()

                    For Each person As Person In PersonList

                        cmd.Parameters("@CompanyName").Value = person.Name
                        cmd.Parameters("@ContactName").Value = person.Contact
                        cmd.Parameters("@Image").Value = person.Image

                        Affected = cmd.ExecuteNonQuery()

                        If Affected = 1 Then
                            cmd.CommandText = "Select @@Identity"
                            person.Id = CInt(cmd.ExecuteScalar)
                        End If

                    Next

                End Using
            End Using
        Catch ex As Exception
            mExceptiom = ex
            Success = False
        End Try

        Return Success

    End Function

End Class
Public Class Person
    Public Property Id As Integer
    Public Property Name As String
    Public Property Contact As String
    Public Property Image As Byte()
End Class


这篇关于[VB.Net]如何将图像数据导入另一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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