rs.AddNew in ado.net

  • Thread starter Thread starter Tascien
  • Start date Start date
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.
 
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
 
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.
 
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.
 
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".
 
Back
Top