Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Dear friends,

If the data is available in the database then update that data ,if not present then insert the data in the database.But here this should be achieved without using any stored procedure.Using c sharp language i need it.


Thanks
Posted
Updated 7-Sep-11 1:20am
v2
Comments
Herman<T>.Instance 7-Sep-11 7:16am    
data is database not c#!

You can do this several ways. For example you can first try to update the data and if no rows are updated insert it.

Also you can first try to fetch the data and then make the decision insert/update.

One way is to use table adapter, get the data first to a dataset (or datatable), then make modifications. After that you can call the data adapters update to reflect the changes to the database.

Also, depending on the situation, you may find MERGE transact sql statement useful.
 
Share this answer
 
Create a connection to the database.
Create a command: SELECT id FROM MyTable WHERE id=idOfRecordToHandle
Run the command: if the number of rows is zero, you need an insert
To Insert:
Create a new command: INSERT INTO MyTable...
To Update:
Create a new command: UPDATE MyTable SET...
Add the appropriate parameter values to whichever command you need.
Run the command.

The actual code depends on the database you are using, but the basics are all there.
 
Share this answer
 
So what is the problem?
Check insert and update data in database using C#[^]
 
Share this answer
 
1st check with the query . then use @@RowCount in Storeprocedure to show the effect , store it in a variable and check it. or use Count(ColName) and total query if result is grater than zero then process .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900