Using SqlDataAdapter for Insert but not Update?

S

Stu

I'm having trouble getting my SqlDataAdapter to NOT overwrite existing
records in my SQL table. I'd like it to perform the Insert on rows
that don't exist, but not update the record if it already exists. I
have it set to ContinueUpdateOnError because I don't want it to throw
an exception if a row already exists, I just want to ignore it. I'm
clearly missing something...
 
R

RayLopez99

I'm having trouble getting my SqlDataAdapter to NOT overwrite existing
records in my SQL table.  I'd like it to perform the Insert on rows
that don't exist, but not update the record if it already exists.  I
have it set to ContinueUpdateOnError because I don't want it to throw
an exception if a row already exists, I just want to ignore it.  I'm
clearly missing something...

This is a SQL question, not really a C# question. And if memory
serves it's a conventional question any book can answer. You have to
play around with your code a bit more, that's all. And you should
learn the ADO.NET Entity Framework, which will make the dinosaur SQL
macro language obsolete in a few years anyway.

RL
 
A

Arne Vajhøj

This is a SQL question, not really a C# question.

It is a .NET framework library question - it has nothing to do
with SQL.
And if memory
serves it's a conventional question any book can answer.

I don't think so.
And you should
learn the ADO.NET Entity Framework, which will make the dinosaur SQL
macro language obsolete in a few years anyway.

SQL is not a macro language.

It is well known that ORM's are good for many purposes but not
good for all purposes. SQL will also be used in the future.

Arne
 
S

Stu

It is a .NET framework library question - it has nothing to do
with SQL.


I don't think so.


SQL is not a macro language.

It is well known that ORM's are good for many purposes but not
good for all purposes. SQL will also be used in the future.

Arne

Yeah, what Arne said. So does anyone have any idea how to suppress
the Update functionality of the .NET Framework SqlDataAdapter object
so that it performs only Inserts and ignores existing records?
 
R

RayLopez99

Yeah, what Arne said.  So does anyone have any idea how to suppress
the Update functionality of the .NET Framework SqlDataAdapter object
so that it performs only Inserts and ignores existing records?

Look it up in a textbook. An INSERT is just that. An UPDATE is just
that. One is for new records and the other is for existing records.

C'mon, do some work. You're wasting more time here than you could
solving the problem.

RL
 
S

Stu

Look it up in a textbook.  An INSERT is just that.  An UPDATE is just
that.  One is for new records and the other is for existing records.

C'mon, do some work.  You're wasting more time here than you could
solving the problem.

RL

You're killing me Ray. If you don't understand the question that I'm
asking then don't respond. Trust me, I understand the difference
between an INSERT and an UPDATE. What I don't understand is how to
get this particular .NET Framework object to use them the way I want.
I have read the documentation and it does not clear things up. I have
googled high and low and not found the answer. I use these groups as
a last resort to answer questions that I can't seem to dig up
elsewhere.
 
V

vanderghast

This is quite dependant of the exact objects you use. As example, you can
replace the automatically generated DataSet.UpdateCommand Command (see Chap.
10 of "Microsoft ADO.Net", by David Sceppa, at Microsoft Press, and probably
at MSDN too); at another level, you can also try using an "archive" method
which simply loops over the rows, checking the rowState:

foreach( MyKindOfRow r in myDataTable.Rows)
{
switch (r.RowState)
{
case System.Data.DataRowState.Added:
...
myTableAdapter.Update( r) ; // add the record
...
break;
case System.Data.DataRowState.Deleted:
...
myTableAdapter.Update( r) ; // delete the record
...
break;
case System.Data.DataRowState.Modified:
case System.Data.DataRowState.Unchanged:
case System.Data.DataRowState.Detached:
// do nothing
break;
default:
throw new Exception("Unexpected RowState value.");
}
}


but observe that your row will still be in a state of being 'modified', so
don't blindly Update the row explicitly (or implicitly) somewhere else, the
effect would be that the modified row would then be updated.



Vanderghast, Access MVP





Look it up in a textbook. An INSERT is just that. An UPDATE is just
that. One is for new records and the other is for existing records.

C'mon, do some work. You're wasting more time here than you could
solving the problem.

RL

You're killing me Ray. If you don't understand the question that I'm
asking then don't respond. Trust me, I understand the difference
between an INSERT and an UPDATE. What I don't understand is how to
get this particular .NET Framework object to use them the way I want.
I have read the documentation and it does not clear things up. I have
googled high and low and not found the answer. I use these groups as
a last resort to answer questions that I can't seem to dig up
elsewhere.
 
R

RayLopez99

You're killing me Ray.  If you don't understand the question that I'm
asking then don't respond.  Trust me, I understand the difference
between an INSERT and an UPDATE.  What I don't understand is how to
get this particular .NET Framework object to use them the way I want.
I have read the documentation and it does not clear things up.  I have
googled high and low and not found the answer.  I use these groups as
a last resort to answer questions that I can't seem to dig up
elsewhere.

LIke Vanderghast said, it's in any book, like the ones by Sceppa.
Sceppa even has a book now on the Entity Framework, and I might get it
since the first books out really sucked.


RL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top