ADO.NET数据库应用程序的运行时异常

时间:2018-10-08 17:35:50

标签: c# ado.net sqlconnection sqlcommand

请帮助您理解这些异常告诉我什么以及如何解决它们?

我已经使用ADO.NET创建了一个应用程序。项目调试器没有显示任何错误,但是,在运行时我会收到这些异常。我想我弄乱了我在“ PersonkartotekDBUtil.cs”文件中“ GetFullContactPersonTreeDB”方法中编写的一些代码行。 在这里,我想做的是通过JOIN原理显示完整的个人概述信息以及Person目录的连接属性。

有人可以纠正我吗?

我怀疑无法正常运行的方法是:

public void GetFullContactPersonTreeDB(ref Person fcpt)
    {
        string fullPersonkartotek = @"SELECT  Person.PersonId, Person.FirstName, Person.MiddleName, Person.LastName, Person.Email, Person.Notes, Person.PrimaryAddress, Person.AlternativeAddresses, Person.TelefonNumbers
                                          Address.AddressID, Address.StreetName, Address.HouseNumber, Address.PostNr, Address.PersonPrimary, Person.AlternativePerson
                                          PostNr.PostNrID, PostNr.PostNumber, PostNr._Country, PostNr._City
                                          City.CityID, City.CityName,
                                          Country.CountryID, Country.CountryCode, Country.CountryName,
                                          Telefon.TelefonID, Telefon.Number, Telefon.TelefonType, Telefon.TelefonProvider, 
                                          Provider.ProviderID, Provider.ProviderName

            FROM      Person INNER JOIN
            Address ON Person.PersonId = Address.Person INNER JOIN
            Telefon ON Person.PersonID = Telefon.Person
            AlternativeAddress ON Person.PersonID = AlternativeAddress.Person
            Address ON Person.PersonID = Address.Person
            WHERE   (Person.PersonID = @PersonId)";


    }

运行该程序后,控制台窗口上的错误消息:

  

System.Data.SqlClient.SqlException(0x80131904):附近的语法不正确   '。'在System.Data.SqlClient.SqlConnection.OnError(SqlException   异常,布尔值breakConnection,操作1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction)位于   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject   stateObj,布尔值调用者HasConnectionLock,布尔值asyncClose)   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,   SqlCommand cmdHandler,SqlDataReader dataStream,   BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject   stateObj,Boolean&dataReady)   System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()在   System.Data.SqlClient.SqlDataReader.get_MetaData()在   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,   RunBehavior runBehavior,字符串resetOptionsString,布尔值   isInternal,布尔值forDescribeParameterEncryption,布尔值   shouldCacheForAlwaysEncrypted)在   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior   cmdBehavior,RunBehavior,runBehavior,布尔returnStream,布尔   异步,Int32超时,任务和任务,布尔asyncWrite,布尔inRetry,   SqlDataReader ds,布尔值describeParameterEncryptionRequest),位于   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior   cmdBehavior,RunBehavior,runBehavior,布尔值returnStream,字符串   方法,TaskCompletionSource`1完成,Int32超时,任务和任务,   布尔值和usedCache,布尔值asyncWrite,布尔值inRetry)   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior   cmdBehavior,RunBehavior,runBehavior,布尔值returnStream,字符串   方法)   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior   行为,String方法)在   System.Data.SqlClient.SqlCommand.ExecuteReader()位于   Infrastructure.PersonkartotekDB.ADONET.PersonkartotekDBUtil.GetFullContactPersonTreeDB(Person&   fcpt)中   C:\ Users \ User \ Desktop \ E18I4DABHandIn2au2016082168 \ Personkartotek \ Infrastructure \ PersonkartotekDB.ADONET \ PersonkartotekDBUtil.cs:line   382 ClientConnectionId:716d2899-cbbe-4aa6-89de-66452d907b66错误   编号:102,状态:1,类别:15

     

未处理的异常:System.Data.SqlClient.SqlException:不正确   '。'附近的语法。在   System.Data.SqlClient.SqlConnection.OnError(SqlException异常,   布尔值breakConnection,操作为1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction),位于   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject   stateObj,布尔值调用者HasConnectionLock,布尔值asyncClose)   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,   SqlCommand cmdHandler,SqlDataReader dataStream,   BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject   stateObj,Boolean&dataReady)   System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()在   System.Data.SqlClient.SqlDataReader.get_MetaData()在   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,   RunBehavior runBehavior,字符串resetOptionsString,布尔值   isInternal,布尔值forDescribeParameterEncryption,布尔值   shouldCacheForAlwaysEncrypted)在   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior   cmdBehavior,RunBehavior,runBehavior,布尔returnStream,布尔   异步,Int32超时,任务和任务,布尔asyncWrite,布尔inRetry,   SqlDataReader ds,布尔值describeParameterEncryptionRequest),位于   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior   cmdBehavior,RunBehavior,runBehavior,布尔值returnStream,字符串   方法,TaskCompletionSource`1完成,Int32超时,任务和任务,   布尔值和usedCache,布尔值asyncWrite,布尔值inRetry)   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior   cmdBehavior,RunBehavior,runBehavior,布尔值returnStream,字符串   方法)   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior   行为,String方法)在   System.Data.SqlClient.SqlCommand.ExecuteReader()位于   Infrastructure.PersonkartotekDB.ADONET.PersonkartotekDBUtil.GetFullContactPersonTreeDB(Person&   fcpt)中   C:\ Users \ User \ Desktop \ E18I4DABHandIn2au2016082168 \ Personkartotek \ Infrastructure \ PersonkartotekDB.ADONET \ PersonkartotekDBUtil.cs:line   449在ApplicationLogic.PersonkartotekApp.ProgramApp()中   C:\ Users \ User \ Desktop \ E18I4DABHandIn2au2016082168 \ Personkartotek \ ApplicationLogic \ PersonkartotekApp.cs:line   14在ProgramApplication.Program.Main(String [] args)中   C:\ Users \ User \ Desktop \ E18I4DABHandIn2au2016082168 \ Personkartotek \ ProgramApplication \ Program.cs:line   10

附件显示了调试器上的异常。 enter image description here

1 个答案:

答案 0 :(得分:0)

好像您缺少几个逗号,也缺少内部联接。

通过在Sql Server Management Studio(SSMS)中运行查询并使用parse(CTRL + F5),可以突出显示问题。

请参见下面的固定代码。 将来值得采用一致的格式来简化您的生活。

SELECT  Person.PersonId, Person.FirstName, Person.MiddleName, Person.LastName, Person.Email, Person.Notes, Person.PrimaryAddress, Person.AlternativeAddresses, Person.TelefonNumbers,
                                      Address.AddressID, Address.StreetName, Address.HouseNumber, Address.PostNr, Address.PersonPrimary, Person.AlternativePerson,
                                      PostNr.PostNrID, PostNr.PostNumber, PostNr._Country, PostNr._City,
                                      City.CityID, City.CityName,
                                      Country.CountryID, Country.CountryCode, Country.CountryName,
                                      Telefon.TelefonID, Telefon.Number, Telefon.TelefonType, Telefon.TelefonProvider, 
                                      Provider.ProviderID, Provider.ProviderName

        FROM      Person 
        INNER JOIN Address ON Person.PersonId = Address.Person 
        INNER JOIN Telefon ON Person.PersonID = Telefon.Person
        INNER JOIN AlternativeAddress ON Person.PersonID = AlternativeAddress.Person
        INNER JOIN   Address ON Person.PersonID = Address.Person
        WHERE   Person.PersonID = @PersonId