SQL Server Invalid column name when adding string value

后端 未结 5 805
一向
一向 2021-01-18 15:23

I\'m new to SQL Server

I\'ve created my table like this:

CREATE TABLE Accidents (
     Id INT NOT NULL PRIMARY KEY IDENTITY,
     GUID VARCHAR(100),         


        
5条回答
  •  余生分开走
    2021-01-18 15:48

    I wonder why he considered "a" as a column name?

    That's not hard to see. You shouldn't develop software, deploy it and hope it runs. You should test it. Extract the executing code into a class method, and call that from your service. When developing, you call this method from a unit test or commandline program or whatever you like to test with.

    Your problem: you don't put quotes around the strings (or varchars if you want) in the query. You would've seen it if you just printed the query string to the console for example.

    But honestly that's the least of your problems. You shouldn't hand-craft SQL. At least use parameterized queries. So let your query be:

    "INSERT INTO Accidents (GUID, Latitude, Longitude, PhotoName) 
                    VALUES (@GUID, @Latitude, @Longitude, @PhotoName)"
    

    And bind the parameters:

    cmd.Parameters.AddWithValue("@GUID", GUID);
    ...
    

提交回复
热议问题