从select语句插入SQL

时间:2012-12-27 21:01:36

标签: sql vb.net

我希望代码做的是从文本框ID号中获取输入然后它获取ID所属的记录并将其插入另一个表但是我更改了SQL语句但仍然按下按钮时没有发生

Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click

Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source= " & Server.MapPath("~/Members.mdb"))

Dim cmd As New OleDbCommand

 Dim i As Integer

cmd.Connection = con

 cmd.CommandType = Data.CommandType.Text

cmd.CommandText = "insert into kfupm_stu_visitor (kfupm_stu_id, stu_name, kfupm_maj_id, kfupm_dep_id, level_id) select * from kfupm_stu where fupm_stu_id like @kfupm_stu_id"
        cmd.Parameters.Add("KFUPM_STU_ID", OleDbType.Integer).Value = "%" & txtSTU_ID.Text & "%"
        con.Open()

        Try
            i = cmd.ExecuteNonQuery()
        Catch ex As Exception

            Response.Redirect("msgpage.aspx?id=1")
        Finally
            con.Close()
            End Try

        Response.Redirect("msgpage.aspx?id=2")

    End Sub

1 个答案:

答案 0 :(得分:7)

如果您使用查询来填充列,则不需要values (...)部分。它应该只是:

insert into kfupm_stu_visitor 
(kfupm_stu_id, stu_name, kfupm_maj_id, kfupm_dep_id, level_id)
select * from 
kfupm_stu where fupm_stu_id like @kfupm_stu_id

当然,假设kfupm_stu中的列与insert into列表中的列完全匹配。