'Invest' 附近的语法不正确

Incorrect syntax near 'Invest'

我 运行 我的代码出现以下错误。任何有助于确定原因的帮助。

Dim cmd_Email As New SqlCommand(
      "SELECT Main.P_Email FROM Invest INNER JOIN Main ON Invest.Ph_num = Main.Ph_num" & _
      "WHERE Invest.P_num =" & Ptcl & "UNION" & _
      "Select Main.P_Email FROM Invest INNER JOIN Main ON Invest.Ass1 = Main.Ph_num" & _
      "WHERE Invest.P_num =" & Ptcl & "UNION" & _
      "SELECT Main.P_Email FROM Invest INNER JOIN Main ON Invest.Ass2 = Main.Ph_num" & _
      "WHERE Invest.P_num = " & Ptcl, connection)
Dim read_Email As SqlDataReader = cmd_Email.ExecuteReader()

在我看来你的 T-SQL 格式不对,因为 Ptcl 变量和其他指令之间没有空格。

Dim cmd_Email As New SqlCommand(
  "SELECT Main.P_Email FROM Invest INNER JOIN Main ON Invest.Ph_num = Main.Ph_num " & _
  "WHERE Invest.P_num =" & Ptcl & " UNION " & _
  "Select Main.P_Email FROM Invest INNER JOIN Main ON Invest.Ass1 = Main.Ph_num " & _
  "WHERE Invest.P_num =" & Ptcl & " UNION " & _
  "SELECT Main.P_Email FROM Invest INNER JOIN Main ON Invest.Ass2 = Main.Ph_num " & _
  "WHERE Invest.P_num = " & Ptcl, connection)

以上关于这些空间的查询已得到修复。但是你最好的选择是参数化你的查询,拥有一个更易读的结构,保护自己免受 SQL 注入,并有一个更健壮的方法来将参数传递给查询本身。

希望对您有所帮助