如何修复错误" SQLexception未被逗号附近的用户代码错误语法处理

时间:2015-09-14 19:35:16

标签: asp.net vb.net visual-studio-2012

有关','的任何帮助错误是非常感谢!

这是我的VB代码:

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.ArgumentException

Partial Class MeetingAdd
    Inherits System.Web.UI.Page

    Public Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        Dim strConnection As String = ConfigurationManager.ConnectionStrings("ServiceLine_CommitteeConnectionString").ConnectionString
        Dim con As New SqlConnection(strConnection)
        Dim cmd As New SqlCommand("insert into ServiceLine_Committee.DBO.tblVotings(Meeting_ID, Committee_ID, Member_Name) , " +
                                       "SELECT C.ID, M.ID, CM.FULL_NAME FROM ServiceLine_Committee.DBO.tblCommitteeName C, " +
                                       "ServiceLine_Committee.DBO.tblMeetings M, ServiceLine_Committee.DBO.tblCommitteeMembers CM " +
                                       "WHERE M.ContractCategory    = C.Contract_Category, " +
                                       "AND M.Committee             = C.Committee_Name, " +
                                       "AND CM.Committee            = M.Committee", con)
        cmd.Connection = con
        cmd.CommandType = CommandType.Text
        con.Open()

        Dim result As Integer = cmd.ExecuteNonQuery()

        con.Close()

    End Sub

End Class

请帮忙。这非常令人沮丧。

2 个答案:

答案 0 :(得分:1)

在您发布的代码中,您多次使用异常','

1>第一行:Member_Name) ,

2>第4行"WHERE M.ContractCategory = C.Contract_Category, " + "AND M.Committee = C.Committee_Name,

尝试:

  Dim cmd As New SqlCommand("insert into ServiceLine_Committee.DBO.tblVotings(Meeting_ID, Committee_ID, Member_Name) " +
                                       " SELECT C.ID, M.ID, CM.FULL_NAME FROM ServiceLine_Committee.DBO.tblCommitteeName C, " +
                                       "ServiceLine_Committee.DBO.tblMeetings M, ServiceLine_Committee.DBO.tblCommitteeMembers CM " +
                                       " WHERE M.ContractCategory   = C.Contract_Category " +
                                       "AND M.Committee             = C.Committee_Name " +
                                       "AND CM.Committee            = M.Committee", con)

  Dim cmd As New SqlCommand("insert into ServiceLine_Committee.DBO.tblVotings(Meeting_ID, Committee_ID, Member_Name) " +
                                               " SELECT C.ID, M.ID, CM.FULL_NAME FROM ServiceLine_Committee.DBO.tblCommitteeName C INNER JOIN  " +
                                               "ServiceLine_Committee.DBO.tblMeetings M ON C.Contract_Category=M.ContractCategory AND C.Committee_Name=M.Committee INNER JOIN ServiceLine_Committee.DBO.tblCommitteeMembers CM ON CM.Committee=M.Committee", con)

EDIT2:

使用 + 符号进行字符串连接不好,您可以使用Escaping in verbatim strings

Dim cmd As New SqlCommand(@"insert into ServiceLine_Committee.DBO.tblVotings
(Meeting_ID, Committee_ID, Member_Name)  SELECT 
C.ID, M.ID, CM.FULL_NAME   FROM   ServiceLine_Committee.DBO.tblCommitteeName C INNER JOIN ServiceLine_Committee.DBO.tblMeetings M ON  C.Contract_Category=M.ContractCategory AND 
C.Committee_Name=M.Committee INNER JOIN ServiceLine_Committee.DBO.tblCommitteeMembers CM ON 
CM.Committee=M.Committee", con)

答案 1 :(得分:0)

错误本身很清楚:它说outlog="C:\temp\out.log" license_path="C:\temp\lic.lic" lmgr_files=false license=true 。调试此方法的最佳方法是将You have an extra comma that SQL doesnt understand syntax for设置为SQLCommandString

之类的变量

这样您就可以看到实际执行的SQL命令是什么。在设置mySQLCommand服务器资源管理器mySQLCommand'. Copy that text directly into a Query from SQL Server Management Studio`并尝试执行它之后,立即设置一个断点。你会发现它失败了。现在,您可以正确地查询并将其重新粘贴回代码中。

其他一些注意事项:您正在从多个表中进行选择,但您没有明确定义关系。例如:

or into

技术上会起作用。但这写得不好。更好的方法是定义名称和汽车的相关方式。像这样:

SELECT Name, Car FROM Names, Cars