I want to know if the next piece of code is correct:
SqlCommand cmd = new SqlCommand( "IF NOT EXISTS(SELECT count(*) from Raspunsuri where id_intrebare=2)" + "Insert INTO Raspunsuri VALUES(@raspuns,@cnp,@data,2,@ip,@idsesiune)" + "else" + "UPDATE Raspunsuri SET <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83f1e2f0f3f6edf0bec3f1e2f0f3f6edf0">[email protected]</a>,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9aaa6ad96a7bca4acbba0aa96b9acbbbaa6a7a8a5f489aaa7b9">[email protected]</a>,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="32565346536d40534142475c410f7256534653">[email protected]</a>,id_intrebare=2,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8c1d8f7dddbcdda95e8c1d8">[email protected]</a>,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82ebe6ddf1e7f1ebf7ece7bfc2ebe6f1e7f1ebf7ece7">[email protected]</a> WHERE id_intrebare=2", con);
All the parameters are correct that I want to insert but it seems this piece of code doesn’t do the insert or update.Do you have any suggestions?it’s a sql query combined with c#..
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Inspect the string that’s created by that command: some words need spaces between them.
SqlCommand cmd = new SqlCommand("IF NOT EXISTS(SELECT 1 from Raspunsuri where id_intrebare=2)" +
" Insert INTO Raspunsuri VALUES(@raspuns,@cnp,@data,2,@ip,@idsesiune)" +
" else" +
" UPDATE Raspunsuri SET <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7200130102071c014f3200130102071c01">[email protected]</a>,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcdfd3d8e3d2c9d1d9ced5dfe3ccd9cecfd3d2ddd081fcdfd2cc">[email protected]</a>,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85e1e4f1e4daf7e4f6f5f0ebf6b8c5e1e4f1e4">[email protected]</a>,id_intrebare=2,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b021b341e180e19562b021b">[email protected]</a>,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f19895ae82948298849f94ccb1989582948298849f94">[email protected]</a> WHERE id_intrebare=2", con);
Method 2
Description
No because you select count that has always a value.
select a column or * instead.
Sample
SqlCommand cmd = new SqlCommand( "IF NOT EXISTS(SELECT id_intrebare from Raspunsuri where id_intrebare=2) " + "Insert INTO Raspunsuri VALUES(@raspuns,@cnp,@data,2,@ip,@idsesiune) " + "else " + "UPDATE Raspunsuri SET <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3446554744415a47097446554744415a47">[email protected]</a>,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="81e2eee5deeff4ece4f3e8e2def1e4f3f2eeefe0edbcc1e2eff1">[email protected]</a>,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dabebbaebb85a8bba9aaafb4a9e79abebbaebb">[email protected]</a>,id_intrebare=2,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f766f406a6c7a6d225f766f">[email protected]</a>,<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cf5f8c3eff9eff5e9f2f9a1dcf5f8eff9eff5e9f2f9">[email protected]</a> WHERE id_intrebare=2", con);
Method 3
Why don’t you use the MERGE command?
Method 4
You can try executing the query in SQL Server Management Studio window first. This will give you an easy way to debug the things
Method 5
I think the blank spaces are missing between the “else” statement
Also ensure that you have provided all the columns in the values part
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0