从其他表vb插入数据网络访问OleDb

问题描述

我在同一数据库上有不同的表,我需要从组合框中插入数据的ID。 这是客户桌

enter image description here

And here's other table

here's my app

我需要的是从组合框选择的项目中获取ID,并将其放在最终表中, 这就是我尝试的

cmd.Parameters.AddWithValue("Client",client.Text)

Private Sub livbtn_Click(sender As Object,e As EventArgs) Handles livbtn.Click
        'ModePaiement()
        Try
            SQL = "INSERT INTO LIVRAISONCLIENTGNC(Codeclient) SELECT code_client from client WHERE client =  @Client "
            Execute(SQL,"Insert")

            MessageBox.Show("The record has been saved.","",MessageBoxButtons.OK,MessageBoxIcon.Information)
            ResetMe()
        Catch ex As Exception
            MessageBox.Show("" & ex.Message,MessageBoxIcon.Exclamation)
        End Try

    End Sub

不能确定是否起作用,请提供帮助!

这是模块数据连接:

Option Explicit On
Option Strict On
Imports System.Data.OleDb

Module AccessDB_Connection

    Public Function GetConnectionString() As String
        Dim strCon As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data source=" & Application.StartupPath & "\BLdatabase.accdb;Persist Security Info = false;"
        Return strCon
    End Function

    Public con As New OleDbConnection(GetConnectionString())
    Public cmd As OleDbCommand
    Public SQL As String = String.Empty

    Public Function PerformCRUD(Com As OleDbCommand) As DataTable
        Dim da As OleDbDataAdapter
        Dim dt As New DataTable()

        Try
            da = New OleDbDataAdapter
            da.SelectCommand = Com
            da.Fill(dt)
            Return dt
        Catch ex As Exception
            MessageBox.Show("" & ex.Message)

        End Try
        Return dt
    End Function

End Module

解决方法

谢谢大家的回复; 所以这是execute方法:

Private Sub Execute(MySQL As String,Optional Parameter As String = "")
    cmd = New OleDbCommand(MySQL,con)
    AddParameters(Parameter)
    PerformCRUD(cmd)
End Sub

 Private Sub AddParameters(str As String)
cmd.Parameters.AddWithValue("Client",client.Text)
End Sub

    Public Function PerformCRUD(Com As OleDbCommand) As DataTable
        Dim da As OleDbDataAdapter
        Dim dt As New DataTable()

        Try
            da = New OleDbDataAdapter
            da.SelectCommand = Com
            da.Fill(dt)
            Return dt
        Catch ex As Exception
            MessageBox.Show("" & ex.Message)

        End Try
        Return dt
    End Function

这很简单,我需要存储所选项目的ID值:

enter image description here

这是一个示例,当我从组合框中选择IMAX客户端时,我需要将该值26从该表客户端存储到其他表 enter image description here enter image description here

有2个不同的结果,这取决于所使用的查询: 与SQL =“插入到LIVRAISONCLIENTGNC(Codeclient)从客户端WHERE客户端= @Client中选择code_client“->没有任何反应 与SQL =“插入到LIVRAISONCLIENTGNC(Codeclient)值(从客户端的WHERE客户端= @Client中选择code_client)” ->错误 enter image description here

,

我看到的问题,您正在尝试运行:

 SQL = "INSERT INTO LIVRAISONCLIENTGNC(Codeclient) SELECT code_client from client WHERE client =  @Client "

但是,在PerformCRUD方法中,您要填充数据适配器并基于INSERT返回DataTable,其中ExecuteNoQuery是要使用的方法。

以下是示例(More Info):

Using cmd = con.CreateCommand()
     cmd.CommandText = SQL
    'It is good practice to specify the data type. Note: 80 in this example is the column size/length.
    cmd.Parameter.Add("@Client",OleDbType.VarChar,80).Value = client.Text
    cmd.Connection = con
    con.Open() 'if not already open
    cmd.ExecuteNonQuery()
End Using

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...