question

elfenliedtopfan5-3426 avatar image
0 Votes"
elfenliedtopfan5-3426 asked ·

c# MySQL check if value exists issue

Hello all,

i have been trying for a while now to check a database to see if a value exists by name in a row,

but keep getting syntax errors and Im totally lost how to get this to work,

this is what i have tried and the code i currently have ( code i tried is commented out )

         public static bool check(string Name)
         {
    
             //MySqlCommand cmd = new MySqlCommand("SELECT COUNT (*) FORM Appplication_Details WHERE FriendlyNameMS='" + Name + "'", conn);   SELECT EXISTS(SELECT * from ExistsRowDemo WHERE ExistId=105   
             MySqlCommand cmd = new MySqlCommand(" SELECT EXISTS(SELECT * from Appplication_Details WHERE FriendlyNameMS='" + Name + "'", conn);
             object obj = cmd.ExecuteScalar();
             if (Convert.ToInt32(obj) > 0)
             {
                 return true;
             }
             else
             {
                 return false;
             }
    
         }

thank you in advance elfenliedtopfan5


sql-server-generaldotnet-csharp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cheong00 avatar image
1 Vote"
cheong00 answered ·

You missed closing ')' in query.

 MySqlCommand cmd = new MySqlCommand(" SELECT EXISTS(SELECT * from Appplication_Details WHERE FriendlyNameMS='" + Name + "')", conn);
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

OlafHelper-2800 avatar image
1 Vote"
OlafHelper-2800 answered ·

SELECT EXISTS (SELECT * from Appplication_Details WHERE …)

For MS SQL Server/T-SQL this construct with EXISTS in the SELECT would be invalid SQL statement and I can't imagine that MySQL supports this.
Your first query in comment ist the right one; beside the typo in FORM => FROM

 MySqlCommand cmd = new MySqlCommand("SELECT COUNT (*) FROM Appplication_Details WHERE FriendlyNameMS='" + Name + "'", conn);

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

elfenliedtopfan5-3426 avatar image
0 Votes"
elfenliedtopfan5-3426 answered ·

Thank you to both of your input both are correct i was just having a stupid moment i have looked back at it today and before even looking on here noticed that i made a mistake with the ) moral of the story don't code at 3:30 am haha

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.