Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a few visual basic functions that I want to move onto my SQL 2000 server. I've never written a stored procedure before, could someone give me a few pointers on how I would execute the following as an SP

 

Public Class SQLDatabase
Private conn As SqlConnection

Public Sub New
conn = New SqlConnection("MyConnectionString")
End Sub

Public Sub openDB()
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
End Sub

Public Sub closeDB()
If Not (conn.State = ConnectionState.Closed) Then
conn.Close()
End If
End Sub

Public Function GetCoOrds(ByVal postcode As String) As String

Try
Dim cmd As New SqlCommand("SELECT grideast,gridnorth From gridrefs WHERE postcode='" & postcode & "'", conn)
Dim rdr As SqlDataReader = cmd.ExecuteReader
If rdr.HasRows Then
While rdr.Read
Return = rdr(0).ToString + "," + rdr(1).ToString
End While
Else
Throw New Exception("No co-ordinates in database for " + postcode)
End If
Catch ex As Exception
Throw ex
End Try
End Function
End Class

 

I'm calling my function simply with

 

Dim db as New SqlDatabase
db.opendb
messagebox.show("the co-ordinates are " + db.GetCoOrds("TQ13OIU")
db.closedb

 

Thanks

Posted

Create Procedure dbo.MyCoords

@postcode as int --i'm assuming postcode is an int

as

SELECT grideast,gridnorth From gridrefs WHERE postcode=@ 

 

make sure to change your OleCommandType to System.Data.CommandType.StoredProcedure

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...