473,503 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataAdapter using stored procedures for Insert, Update and Delete

RB
Hi All,

I'm trying to use more data-adapters/data-sets in a project, because
they are so awesome! Anyhoo, I want to use them with Stored Procedures,
rather than raw SQL text.

The hard part of working with SPs is passing the new values to the
insert, update and delete stored procedures. I've figured out one way to
do it, but was wondering if anyone else had any better ideas, as my way,
although it works fine, means writing quite a lot of code...

My way is to declare my data adapater globally and WithEvents, then use
the RowUpdating event to set the SQLCommand's parameters (see code for
example)

My question then is : Is this a reasonable practice, or is there a much
better way I'm too dense to see?

Cheeers,

RB.

============= SAMPLE CODE ================

' NOTE : Sample code to illustrate a point.
' Probably won't compile or anything.
Imports System.Data
Imports System.Data.SqlClient

Public Class Employee
Protected WithEvents da As New SqlDataAdapter()

Public Function GetTenantAccount(ByVal clientId As Integer) As
DataTable
Dim dt As New DataTable()
Dim scSelect As New SqlCommand("sp_SelectEmployee", conn)
Dim scUpdate As New SqlCommand("sp_UpdateEmployee", conn)
Dim scInsert As New SqlCommand("sp_InsertEmployee", conn)
Dim scDelete As New SqlCommand("sp_DeleteEmployee", conn)

scSelect.CommandType = CommandType.StoredProcedure
scUpdate.CommandType = CommandType.StoredProcedure
scInsert.CommandType = CommandType.StoredProcedure
scDelete.CommandType = CommandType.StoredProcedure

da = New SqlDataAdapter(scSelect)
da.Fill(dt)
da.UpdateCommand = scUpdate
da.InsertCommand = scInsert
da.DeleteCommand = scDelete

Return dt
End Function

Private Sub da_RowUpdating(ByVal sender As Object, _
ByVal e As System.Data.SqlClient.SqlRowUpdatingEventArgs) _
Handles da.RowUpdating
Select Case e.Row.RowState
Case DataRowState.Deleted
Dim sc As SqlCommand = da.UpdateCommand
sc.Parameters("EmployeeId").Value = e.Row("EmployeeId")
Case DataRowState.Modified
Dim sc As SqlCommand = da.UpdateCommand
sc.Parameters("EmployeeId").Value = e.Row("EmployeeId")
sc.Parameters("FirstName").Value = e.Row("FirstName")
Case DataRowState.Added
Dim sc As SqlCommand = da.InsertCommand
sc.Parameters("EmployeeId").Value = e.Row("EmployeeId")
sc.Parameters("FirstName").Value = e.Row("FirstName")
End Select
End Sub
End Class
Dec 5 '07 #1
1 3633
RB,

What is your reason that you are updating every row change. Be aware that in
a multiuser environment you never get the updates cq changes that are be
done withouth a real select.

Normal you would update a datatable just as everything is done. (Be aware
that if it is in a relation you have to do the parents or child first,
depending of the rowstates)

However probably you have a reason for this.

Cor

Dec 5 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1968
by: Brad H McCollum | last post by:
I've looked through many suggestions and partial examples all over this newsgroup and still am not coming up with anything that does specifically what I'm wanting to accomplish. I'm writing a VB...
1
1186
by: Vik | last post by:
In SQL Server, there is a table with PK and a simple view sorting this table. In designer, I create a dataadapter based on this view and get an error message for Update and Delete statements:...
13
2062
by: Doug Bell | last post by:
Hi, I thought I had this sorted this morning but it is still a problem. My application has a DataAccess Class. When it starts, it: Connects to a DB (OLE DB) If it connects it uses an...
1
1366
by: Franklin M. Gauer III | last post by:
In VS2003 I used to be able to drag a SQL Server table from Server Explorer onto my webservice (in design mode) and it would automatically create a DataAdapter for me. This doesn't work in VS2005...
2
1872
by: Franklin M. Gauer III | last post by:
I create a simple DATAADAPTER in a webservice project. It creates the UPDATE, INSERT, DELETE commands for me - no problem. In vS2005 it creates these commands as RESOURCES in the RESX file (i.e....
6
13979
by: Rich | last post by:
Dim da As New SqlDataAdapter("Select * from tbl1", conn) dim tblx As New DataTable da.Fill(tblx) '--works OK up to this point da.UpdateCommand = New SqlCommand da.UpdateCommand.Connection =...
2
3638
by: =?Utf-8?B?VEQgaXMgUFNQ?= | last post by:
I have a lookup table with and ID field and a Description field, and another table that has its own ID field, the ID field from the lookup table, and other data. LookupTable LookupTable.ID...
2
1768
by: Andy B | last post by:
Is there an easy way to convert tableAdaptor queries into stored procs without messing up the dataTables in the dataSet or losing the queries themselves?
6
2928
by: insirawali | last post by:
Hi all, I have this problem, i need to know is there a way i cn use the data adapter's update method in this scenario. i have 3 tables as below create table table1{ id1 int identity(1,1)...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.