C# - Web Site - SQL Select Statement

后端 未结 5 1984
深忆病人
深忆病人 2021-01-24 16:29

I want to use a select statement to find if there is a record that already exists. I\'ve put the code below but it throws an error at the dReader = comm.ExecuteReader(); and i\'

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-24 16:32

    Change your == to =. That is invalid SQL as it is.

    Also if txtID.Text is non-numeric then it needs to be in single quotes. You should not be constructing your SQL like this, instead use a parameter:

    comm.CommandText = "SELECT * FROM Customers WHERE CustomerID = @CustomerID";
    comm.Parameters.AddWithValue("CustomerID", txtID.Text);    
    

    More Info

    C# using statement

    SQL reference

    SQL injection (why you should parameterize your queries)

提交回复
热议问题