R
Ray
Hello.
I'm trying to write records to a database, if they are not there I add them new and if they are there I update them. I'm looking for a sensible approach to this in ADO.NET.
Previoulsy I was using adodb code like this where "filename" can't be a duplicate...
(Of course typically these code blocks would be put into loops and the hard coded values would be variables)
Try
cmd.CommandText = "INSERT INTO simple_list (filename, owner)VALUES ('mydoc.txt','Ted')"
Catch
cmd.CommandText = "UPDATE simple_list SET simple_list.owner = 'Ted' WHERE simple_list.filename='mydoc.txt"
End Try
Can anyone tell me how to get similar results in ADO.NET? Currently I'm on this path which works if the record isn't already there...
Dim cs As String = "Provider='Microsoft.Jet.OLEDB.4.0';Data Source='../database/list.mdb'"
Dim cNet As New System.Data.OleDb.OleDbConnection(cs)
Dim da As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM simple_list", cNet)
Dim ds As New DataSet
Dim dr As DataRow
Try
Dim cmdbldr As New System.Data.OleDb.OleDbCommandBuilder(da)
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
da.Fill(ds, "simple_list")`
dr = ds.Tables("simple_list").NewRow()
dr("filename") = 'mydoc.txt'
dr(UserName) = 'Ted'
ds.Tables("simple_list").Rows.Add(dr)
da.Update(ds, "simple_list")
da.Update(ds, "simple_list")
Catch ex As Exception
End Try
I'm trying to write records to a database, if they are not there I add them new and if they are there I update them. I'm looking for a sensible approach to this in ADO.NET.
Previoulsy I was using adodb code like this where "filename" can't be a duplicate...
(Of course typically these code blocks would be put into loops and the hard coded values would be variables)
Try
cmd.CommandText = "INSERT INTO simple_list (filename, owner)VALUES ('mydoc.txt','Ted')"
Catch
cmd.CommandText = "UPDATE simple_list SET simple_list.owner = 'Ted' WHERE simple_list.filename='mydoc.txt"
End Try
Can anyone tell me how to get similar results in ADO.NET? Currently I'm on this path which works if the record isn't already there...
Dim cs As String = "Provider='Microsoft.Jet.OLEDB.4.0';Data Source='../database/list.mdb'"
Dim cNet As New System.Data.OleDb.OleDbConnection(cs)
Dim da As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM simple_list", cNet)
Dim ds As New DataSet
Dim dr As DataRow
Try
Dim cmdbldr As New System.Data.OleDb.OleDbCommandBuilder(da)
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
da.Fill(ds, "simple_list")`
dr = ds.Tables("simple_list").NewRow()
dr("filename") = 'mydoc.txt'
dr(UserName) = 'Ted'
ds.Tables("simple_list").Rows.Add(dr)
da.Update(ds, "simple_list")
da.Update(ds, "simple_list")
Catch ex As Exception
End Try