reusing code for repeated database use

M

Martin Eyles

Hi,
I am writing a application in ASP.net (using vb.net for my code behind).
I have many queries, all of which are against just 2 databases on a server.
This server can change, and the name is stored in an XML config file. I have
quite a bit of code, and would really like to reuse some of it. I think this
is the kind of thing that would warant making my own object for data access.
Does anyone have any tutorials, hints or best practices on doing this? I
have provided an example of my usage below.

Thanks,
Martin

--------------------CODE--------------------

'This is always the same
aConnectionString = "Data Source=" & ServerName & ";" _
& "Database=DB1;UID=UID1;PWD=PWD1"
conn = New System.Data.SqlClient.SqlConnection
conn.ConnectionString = aConnectionString
conn.Open()

'This changes
SQL = "some SQL Code"

'This is always the same
anSQLCommand = New System.Data.SqlClient.SqlCommand
anSQLCommand.Connection = conn
anSQLCommand.CommandText() = SQL

'This changes
R = anSQLCommand.ExecuteReader()
While R.Read
If Not (IsDBNull(R("aVariable"))) Then
aVariable = CDbl(R("aVariable"))
End If
End While

'This is always the same
conn.Close()
 

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,574
Members
45,048
Latest member
verona

Latest Threads

Top