sql - C# SQLCommand does not write data into db -


i have following sql statement (which works if execute inside db):

if exists (select * dbo.popularitypokemon dex_id = '445')    update dbo.popularitypokemon set timespicked = timespicked + 1 else    insert dbo.popularitypokemon (dex_id, timespicked)values('445', 1); 

i need code-behind.

i have following 2 parameters:

private const int _pickcount = 1; string dexid 

the dexid parameter given function call.

doing standard procedure making new connection , opening works fine, possibly have syntax error when using sqlcommand:

try {     using (myconnection)     {         using (sqlcommand command = new sqlcommand())         {             command.connection = myconnection;             command.commandtype = commandtype.text;             command.commandtext = "if exists (select * dbo.popularitypokemon dex_id = @dex_id) update dbo.popularitypokemon set timespicked = timespicked + @pickcount"                 + "else"                  + "insert dbo.popularitypokemon (dex_id, timespicked)"                 + "values(@dex_id, @pickcount)";             command.parameters.addwithvalue("@dex_id", dexid);             command.parameters.addwithvalue("@pickcount", _pickcount);             command.executenonquery();         }     } } 

am missing something?

i think need put white space @ end of every string creates command.

command.commandtext = "if exists (select * dbo.popularitypokemon dex_id = @dex_id) update dbo.popularitypokemon set timespicked = timespicked + @pickcount "             + "else "              + "insert dbo.popularitypokemon (dex_id, timespicked) "             + "values(@dex_id, @pickcount)"; 

or use verbatim string literal as;

command.commandtext = @"if exists (select * dbo.popularitypokemon dex_id = @dex_id) update dbo.popularitypokemon set timespicked = timespicked + @pickcount                          else                          insert dbo.popularitypokemon (dex_id, timespicked)                          values(@dex_id, @pickcount)"; 

also don't use addwithvalue as can. it may generate unexpected , surprising results sometimes. use add method overload specify parameter type (sqldbtype) , it's size.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -