c# MySQL check if value exists issue

elfenliedtopfan5 121 Reputation points
2021-03-03T03:22:17.743+00:00

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
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,666 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,216 questions
0 comments No comments
{count} votes

Accepted answer
  1. Cheong00 3,471 Reputation points
    2021-03-03T03:42:51.917+00:00

    You missed closing ')' in query.

    MySqlCommand cmd = new MySqlCommand(" SELECT EXISTS(SELECT * from Appplication_Details WHERE FriendlyNameMS='" + Name + "')", conn);
    
    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Olaf Helper 40,736 Reputation points
    2021-03-03T07:03:52.533+00:00

    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);
    
    1 person found this answer helpful.
    0 comments No comments

  2. elfenliedtopfan5 121 Reputation points
    2021-03-03T13:08:30.603+00:00

    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

    0 comments No comments