我的问题是,在一个按钮(单击事件)中,我需要将Table1中的数据(ToolName)复制到Table2(到ToolName)并将描述插入到同一行。
表1 ID - 工具名称 - 数量
表2 ID - 工具名称 - 说明
这是我的代码
Dim sqlquery As String = "INSERT INTO Table2 (ToolName) SELECT ToolName FROM Table1 WHERE ID = '" & DataGridView1.CurrentRow.Cells(0).Value.ToString & "' INSERT INTO Table2 (Description) VALUES ('" & TextBox1.Text & "')"
Dim cmd As New OleDbCommand(sqlquery, con)
con.Open()
cmd.ExecuteNonQuery()
MsgBox(" succesfully", vbInformation)
con.Close()
答案 0 :(得分:0)
更改查询语法,如下所示
Dim sqlquery As String = "INSERT INTO Table2 (ToolName)
SELECT ToolName FROM Table1
WHERE ID = '" & DataGridView1.CurrentRow.Cells(0).Value.ToString & "';
INSERT INTO Table2 (Description) VALUES ('" & TextBox1.Text & "')"
我认为缺少的查询没有错;在2个插入查询之间。
答案 1 :(得分:0)
参数化查询有两个主要优点:
试试这个
Dim sqlquery As String= "INSERT INTO Table2 (ToolName,Descrption) SELECT ToolName,@Desc FROM Table1 WHERE ID = @Id"
Dim cmd As New OleDbCommand(sqlquery, con)
cmd.Parameters.Add("@Desc", SqlDbType.VarChar, 50).Value = TextBox1.Text
cmd.Parameters.Add("@Id", SqlDbType.VarChar, 50).Value = DataGridView1.CurrentRow.Cells(0).Value.ToString
con.Open()
cmd.ExecuteNonQuery()
MsgBox("succesfully", vbInformation)
con.Close()
答案 2 :(得分:0)
根据您已有的内容将其分解为两个单独的更新,使用以下代码并将ToolName传递到更新语句中
Dim Table_ As String = "getToolName"
Dim query As String = "SELECT ToolName FROM Table1 WHERE ID = '" & DataGridView1.CurrentRow.Cells(0).Value.ToString
Dim cmd As New OleDbCommand(query, con)
Dim da As New OleDbDataAdapter(cmd)
da.Fill(ds, Table_)
Dim dt As DataTable = ds.Tables(Table_)
Dim ToolName As String = dt.Rows(0)(0).ToString()