I'm not sure if this belongs in the database section or the NET section. I will leave that up to the moderators.
I have converted a few of my VB6 systems to NET. However, I have retained the ADO coding technique. I probably need to update that. Below is a sample from one of my applications. As you can see, I retrieve the data and, based on conditions, I then issue an UPDATE command. From what I have been reading (and not really grasping) a DATASET/DATA ADAPTER combination will do the same thing. If so, could somebody rewrite this small block of code using "proper" ADO.NET techniques. If I see my own code rewritten it will be much more meaningful to me. Thanks in advance.
Code:Dim cnxn As New SqlClient.SqlConnection(cnxnstring) cnxn.Open() Dim sql As New SqlClient.SqlCommand sql.CommandText = "SELECT * FROM PART WHERE LTRIM(RTRIM([PARTNUMBER])) = " & "'" & Trim([PARTNUMBER]) & "'" sql.CommandType = CommandType.Text sql.Connection = cnxn Dim reader As SqlDataReader reader = sql.ExecuteReader Using reader If reader.HasRows Then reader.Read() ' Dim cnxnupdate As New SqlClient.SqlConnection(cnxnstring) cnxnupdate.Open() Dim sqlupdate As New SqlClient.SqlCommand If Not IsDBNull(reader("BALANCE")) Then sqlupdate.CommandText = "UPDATE PART SET [BALANCE] = [BALANCE] - " & CInt(txtQuantity.Text) Else sqlupdate.CommandText = "UPDATE PART SET [BALANCE] = " & CInt(txtQuantity.Text) * -1 End If sqlupdate.CommandType = CommandType.Text sqlupdate.Connection = cnxnupdate sqlupdate.ExecuteNonQuery() sqlupdate.Dispose() cnxnupdate.Close() cnxnupdate.Dispose() '==================== End If End Using sql.Dispose() reader.Close() cnxn.Close() cnxn.Dispose() '=================== MsgBox("Record added", MsgBoxStyle.Information)




Reply With Quote