Change

S

shapper

Hello,

I need to get a value which might be on a SQL database or Access
database.

My problem is I need to know which is the database type or, even
better, create a code which would work for both databases.

Anyway, first of all I am getting my connection string from Web.Config:

Dim connectionString As String =
ConfigurationManager.ConnectionStrings("Conn").ConnectionString

Then, if the database is SQL I have the code:

' Define connection
Dim connection As New
SqlClient.SqlConnection(connectionString)

' Define command
Dim command As New SqlClient.SqlCommand
With command
.CommandText = "GetValue"
.Connection = connection
.CommandType = CommandType.StoredProcedure
End With

' Add command parameters
With command.Parameters
.Add(New SqlClient.SqlParameter("@Culture", culture))
End With

' Execute the command
connection.Open()
Dim content As SqlClient.SqlDataReader =
command.ExecuteReader
If content.Read Then
Return content.Item("Value").ToString()
Else
Return "# Undefined Value#"
End If
content.Close()
connection.Close()

If the database is Access there are a few changes just in types as
follows:

' Define connection
Dim connection As New
OleDb.OleDbConnection(Me.ConnectionString)

' Define command
Dim command As New OleDb.OleDbCommand
With command
.CommandText = "GetValue"
.Connection = connection
.CommandType = CommandType.StoredProcedure
End With

' Add command parameters
With command.Parameters
.Add(New OleDb.OleDbParameter("@ContentCulture", culture))
End With

' Execute the command
connection.Open()
Dim content As OleDb.OleDbDataReader = command.ExecuteReader
If content.Read Then
Return content.Item("Value").ToString()
Else
Return "# Undefined Value #"
End If
content.Close()
connection.Close()

How should I create this?

Thanks,

Miguel
 

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
474,262
Messages
2,571,059
Members
48,769
Latest member
Clifft

Latest Threads

Top