C# SQL data check like in PHP???
-
Hi, I have a problem, I want to check a data is entered to SQL database or not. I am making this in PHP like below:
$sql_name_check = mysql_query("SELECT name FROM names WHERE name='$buffer_name'"); if(!mysql_num_rows($sql_name_check)){ //Do this do that... }
So I want to do that in C# so I made a code block which is below:int k=0; string buffer_name = "Cem Louis"; SqlConnection mySqlConnection = new SqlConnection("Initial Catalog=names;Data Source=localhost;Integrated Security=SSPI;"); SqlCommand mySqlCommand = mySqlConnection.CreateCommand(); mySqlCommand.CommandText = "SELECT name, tel FROM names WHERE tel = '1' AND name = name"; mySqlConnection.Open(); SqlDataReader mySqlDataReader3 = mySqlCommand.ExecuteReader(); while(mySqlDataReader3.Read()) { k++; } if(k > 0) //Do this do that... return false; else return true; mySqlDataReader3.Close(); mySqlConnection.Close();
Thank you; Cem Louis... -
Hi, I have a problem, I want to check a data is entered to SQL database or not. I am making this in PHP like below:
$sql_name_check = mysql_query("SELECT name FROM names WHERE name='$buffer_name'"); if(!mysql_num_rows($sql_name_check)){ //Do this do that... }
So I want to do that in C# so I made a code block which is below:int k=0; string buffer_name = "Cem Louis"; SqlConnection mySqlConnection = new SqlConnection("Initial Catalog=names;Data Source=localhost;Integrated Security=SSPI;"); SqlCommand mySqlCommand = mySqlConnection.CreateCommand(); mySqlCommand.CommandText = "SELECT name, tel FROM names WHERE tel = '1' AND name = name"; mySqlConnection.Open(); SqlDataReader mySqlDataReader3 = mySqlCommand.ExecuteReader(); while(mySqlDataReader3.Read()) { k++; } if(k > 0) //Do this do that... return false; else return true; mySqlDataReader3.Close(); mySqlConnection.Close();
Thank you; Cem Louis... -
:) And my question is: How can I know that the SQL query returns a result? Is the cell filled with data??? The C# code that I wrote above is not doing this with variable k++ increment... Thanks...
-
If you have any rows returned SqlDataReader has property HasRows SO you can do: if (reader.HasRows) { // something returned }