Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# checking if record exists in SQL error

Tags:

c#

.net

sql

I am using this code to check whether a value (guid1) already exists in the 'guid' table:

string selectString = "SELECT guid" + "FROM trafficScotland" + "WHERE guid = " + guid1;

SqlCommand myCommand = new SqlCommand(selectString, myConnection);
String strResult = String.Empty;
strResult = (String)myCommand.ExecuteScalar();

 if (strResult.Length == 0)

But at the

 strResult = (String)myCommand.ExecuteScalar();

line, I get the sqlException error

Incorrent syntax near 'guid'

Please show me what is wrong here?

like image 593
Dan Sewell Avatar asked Jul 14 '11 14:07

Dan Sewell


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

"SELECT guid" + "FROM trafficScotland" + "WHERE guid ="

That's:

SELECT guidFROM trafficScotlandWHERE guid =

It makes no sense to break that down into separate strings anyway, but you are missing spaces between words :)

string resultGuidAsString = null;

// build command object
string cmdQuery = "SELECT guid FROM trafficScotland WHERE guid=@guid";
SqlCommand myCmd = new SqlCommand(cmdQuery, myConnection);

// safely pass in GUID parameter value
myCmd.Parameters.AddWithValue("@guid", guid1);

// read result, check for nulls in DB
object result = myCmd.ExecuteScalar();
if (result != DBNull.Value && result != null)
{
    resultGuidAsString = result.ToString();
}

^^ Here's an improved version. Several points for criticism if I may:

  • No parameters were used for your query: just building one string. A security, readability and maintainability risk
  • Presumably you're checking whether there is an entry with that guid, suggesting there might not be, but you're not checking for DBNull.Value in case there isn't
  • Just a bit confusing - you're returning a string but dealing with Guids. Odd.
like image 186
Kieren Johnstone Avatar answered Nov 01 '22 16:11

Kieren Johnstone


Do something like this instead:

var selectString = "SELECT 1 FROM trafficScotland WHERE guid = @guid"
var myCommand = new SqlCommand(selectString, myConnection);
myCommand.Parameters.AddWithValue("@guid", guid1);

var itExists = (Int32)myCommand.ExecuteScalar() > 0;
if (itExists) {
    // do stuff...
}
like image 34
Yuck Avatar answered Nov 01 '22 15:11

Yuck