SQL Server Invalid column name when adding string value

后端 未结 5 804
一向
一向 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:28

    I'm guessing your GUID value starts with an a. If it started with a 3 you'd probably get something more entertaining.

    Since you're passing it in as a string, and not escaping it with quotes, you receive an error.

    SqlCommand cmd = new SqlCommand("INSERT INTO Accidents (GUID,Latitude,Longitude,PhotoName) VALUES ("
                + "\"" + GUID + "\"," + latitude + "," + longitude + ",\"" + photoName + "\")", con);
    

    A few points:

    1. You should really avoid building queries in this way. Use a stored procedure or parameterized query.

    2. why are you storing a guid as a varchar()? There is a very nice Guid datatype available.

    3. Don't name columns after data types.

提交回复
热议问题