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
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