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?
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 ...
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.
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.
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.
"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:
DBNull.Value
in case there isn'tstring
but dealing with Guid
s. Odd.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...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With