rs.AddNew in ado.net

T

Tascien

'hi, this code is easy and straight forward in ADODB. if ADO.NET is easier
'and requires less code writing..., how do i write the same code...

rs.Open "SELECT * FROM tb_City WHERE name='Vancouver'", myConn, 3,4

if rs.Eof then
rs.AddNew
End if

rs("LastViewed")=Now()
rs.UpdateBatch()

' To keep in mind...
' I want to check if a vancouver exists, if not, add it, if yes, just update
' the record that is there.
 
C

Carl Prothman [MVP]

Tascien said:
'hi, this code is easy and straight forward in ADODB. if ADO.NET is
easier 'and requires less code writing..., how do i write the same
code...

rs.Open "SELECT * FROM tb_City WHERE name='Vancouver'", myConn, 3,4

if rs.Eof then
rs.AddNew
End if

rs("LastViewed")=Now()
rs.UpdateBatch()

' To keep in mind...
' I want to check if a vancouver exists, if not, add it, if yes, just
update ' the record that is there.

You could use a DataSet / DataAdapter as follows:

' Get the 'ABLEC' customer
SqlDataAdapter1.SelectCommand.Parameters("@CustomerID").Value = "ABLEC"
SqlDataAdapter1.Fill(DataSet1, "Customers")

' If we found the 'ALFKI' record
If DataSet1.Tables("Customers").Rows.Count > 0 Then
' Update the record
DataSet1.Tables("Customers").Rows(0)("ContactName") = "Carl Prothman"
Else
' Insert a new record
Dim newRow As DataRow = DataSet1.Tables("Customers").NewRow()
newRow("CustomerID") = "ABLEC"
newRow("CompanyName") = "Able Consulting, Inc."
DataSet1.Tables("Customers").Rows.Add(newRow)
End If
SqlDataAdapter1.Update(DataSet1)

Where SqlConnection, SqlDataAdapter1, and DataSet1 are objects
on the Form.

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP

Hire top-notch developers at
http://www.able-consulting.com
 
S

Scott M.

Who told you that ADO.NET would require less code? In some cases, this is
true, in many others it is not.

ADO.NET is a much more powerful object model than classic ADO. This
provides better performance and a wider variety of what can be done with
data. But, because there is so much more available, it may very well mean
that more code will need to be written.
 
T

Tascien

Thank you for your input. I am learning this 'brand new' language that
microsoft calls 'Visual Basic'.net but, it does not seem to work and
as easy as VB. So, you understand my pain!

But, i am getting the hang of it now, i hope i will get better.

T.
 
S

Scott M.

I "feel your pain" as they say. The best advice I can give is not to try to
say to yourself "I used to do it this way so it must be done the same way in
..NET".
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top