Create, Retrieve, Update, Delete - CRUD function using adapter and dataset

By bryan tugade

Here's a clever way to create CRUD function using adapter and dataset.

Public Function yourFunction(byval firstparam as string, byval secondparam as string) As DataSet

Dim sqlconn As New SqlConnection(connectionString)
Try
If sqlconn.State = ConnectionState.Closed Then
sqlconn.Open()
End If
Dim adapterz As New SqlDataAdapter()
Dim selectCMD As SqlCommand = New SqlCommand("yourstoredprocedure", sqlconn)
selectCMD.CommandType = CommandType.StoredProcedure
adapterz.SelectCommand = selectCMD
selectCMD.Parameters.Add("@firstparam", SqlDbType.Int).Value = firstparam
selectCMD.Parameters.Add("@secondparam", SqlDbType.VarChar, 3).Value = secondparam

Dim compDS As DataSet = New DataSet

adapterz.Fill(compDS)
compDS = compDS
adapterz.Dispose ()
sqlconn.Close()


Return compDS

Catch ex As Exception
'handle error here
Finally
sqlconn.Close()
End Try


End Function

Thats it, This really works. Happy Coding.

Create, Retrieve, Update, Delete - CRUD function using adapter and dataset  (1264 Views)