Making functions and procedures in ASP.NET

Ø

Øyvind Isaksen

In asp 3.0 I used an procedure (that I included) to save and update
articles, like this:

<%
Sub ConnObj(SQL)
set cn = server.CreateObject("ADODB.Connection")
cn.Open ConnString
cn.Execute SQL
cn.Close
Set cn = nothing
End Sub

'Just call the procedure with SQL an input:
call ConnObj(SQL)
%>


I would like to to the same thing i ASP.NET, but dont know the right way to
do it. I wonder what to do with the "parameters", since the SQL needs to be
an input?
In this procedure, I would write "Dim SQL as string = inputSQL". But I dont
know what to do with the parameters? Is there a smart way to make a
procedure that I can use to execute a SQL, or do I need to write the entire
code each time?

PS: I need a procedure I can use for different SQL's (and databasefields)...
if it is possible without making a new "spesific" procedure based on what I
need to save?


------------------------
Example:
-----------------------

Private Sub SaveArticle(inputSQL)
Dim conn As New SqlConnection(variables.ConnString)
Dim SQL as string = "insert into tblEmployee (fname,lname) values
(@fname,@lname)"
Dim cmd As New SqlCommand(SQL, conn)

Dim parameter1 As New SqlParameter("@fname", Me.txtFname.Text)
cmd.Parameters.Add(parameter1)

Dim parameter2 As New SqlParameter("@lname", Me.txtLname.Text)
cmd.Parameters.Add(parameter2)

cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
cmd.Dispose()
conn.Dispose()
End Sub
 
K

Karl Seguin [MVP]

public shared sub ExecuteRawSql(byval sql as string)
dim connection as new
SqlConnection(_ConnectionStringFromConfigOrSomething)
dim command as new SqlCommand(sql, connection)
try
connection.Open()
command.ExecuteNonQuery()
finally
connection.Dispose()
command.Dispose()
end try
end sub

Of course, if you are serious about having a reusable data access layer,
consider getting the Enterprise LIbrary Data Access Application. This is a
free .NET library provided by microsoft for just what you are trying to do.

http://aspnet.4guysfromrolla.com/articles/070203-1.aspx
http://aspnet.4guysfromrolla.com/articles/030905-1.aspx
http://codebetter.com/blogs/david.hayden/archive/2005/03/09/59533.aspx


Karl
 
Ø

Øyvind Isaksen

Thank you, this was very good information for me!!



"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:[email protected]...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top