emi_sastra
asked on
EXECUTE SCALAR WITH PARAMETER
Hi All,
I have below code :
Public Function Get_Member_Name(ByVal strMemberId As String) As String
Try
Dim dic As New Dictionary(Of String, String)
dic.Add("@MemberId", strMemberId)
sbdSQLCommand.Length = 0
sbdSQLCommand.AppendLine(" SELECT")
sbdSQLCommand.AppendLine(" ISNULL(Ful lName, '') AS FullName")
sbdSQLCommand.AppendLine(" FROM TMMEMBERDATA ")
sbdSQLCommand.AppendLine(" WHERE MemberId = @MemberId")
Using conApp As SqlConnection = New SqlConnection(strSQLCompan yConnectio n)
conApp.Open()
Dim cmdSqlCommand As New SqlCommand
cmdSqlCommand = ERV_Global_Web.Query_Data( dic)
cmdSqlCommand.Connection = conApp
cmdSqlCommand.CommandText = sbdSQLCommand.ToString
cmdSqlCommand.CommandType = CommandType.Text
Get_Member_Name = cmdSqlCommand.ExecuteScala r
If Get_Member_Name Is Nothing Then Get_Member_Name = ""
Return Get_Member_Name
End Using
Catch ex As Exception
End Try
Return ""
End Function
Public Shared Function Query_Data(ByVal dctOfParameterVariables As Dictionary(Of String, String)) As SqlCommand
Dim cmdSqlCommand As New SqlCommand
Dim pair As KeyValuePair(Of String, String)
For Each pair In dctOfParameterVariables
cmdSqlCommand.Parameters.A ddWithValu e(pair.Key , _
pair.Value)
Next
Return cmdSqlCommand
End Function
What's wrong with my code ?
Thank you.
I have below code :
Public Function Get_Member_Name(ByVal strMemberId As String) As String
Try
Dim dic As New Dictionary(Of String, String)
dic.Add("@MemberId", strMemberId)
sbdSQLCommand.Length = 0
sbdSQLCommand.AppendLine("
sbdSQLCommand.AppendLine("
sbdSQLCommand.AppendLine("
sbdSQLCommand.AppendLine("
Using conApp As SqlConnection = New SqlConnection(strSQLCompan
conApp.Open()
Dim cmdSqlCommand As New SqlCommand
cmdSqlCommand = ERV_Global_Web.Query_Data(
cmdSqlCommand.Connection = conApp
cmdSqlCommand.CommandText = sbdSQLCommand.ToString
cmdSqlCommand.CommandType = CommandType.Text
Get_Member_Name = cmdSqlCommand.ExecuteScala
If Get_Member_Name Is Nothing Then Get_Member_Name = ""
Return Get_Member_Name
End Using
Catch ex As Exception
End Try
Return ""
End Function
Public Shared Function Query_Data(ByVal dctOfParameterVariables As Dictionary(Of String, String)) As SqlCommand
Dim cmdSqlCommand As New SqlCommand
Dim pair As KeyValuePair(Of String, String)
For Each pair In dctOfParameterVariables
cmdSqlCommand.Parameters.A
pair.Value)
Next
Return cmdSqlCommand
End Function
What's wrong with my code ?
Thank you.
What happens?
ASKER
Always return nothing.
Thank you.
Thank you.
I can't see anything obviously wrong... I'd step through it in the debugger and confirm that the right values are being passed.
ASKER
I am sorry, it works.
I have simplified into :
Public Function Get_Member_Name(ByVal strMemberId As String, _
Optional ByVal blnMemberName As Boolean = False) As String
Try
sbdSQLCommand.Length = 0
sbdSQLCommand.AppendLine(" SELECT")
If blnMemberName Then
Else
sbdSQLCommand.AppendLine(" ISNULL(Ful lName, '') AS FullName")
End If
sbdSQLCommand.AppendLine(" FROM TMMEMBERDATA ")
sbdSQLCommand.AppendLine(" WHERE MemberId = @MemberId")
Dim dic As New Dictionary(Of String, Object)
dic.Add("@MemberId", strMemberId)
Return ERV_Global_Web.Execute_Sca lar(sbdSQL Command.To String, dic, strSQLCompanyConnection)
Catch ex As Exception
End Try
Return ""
End Function
Public Shared Function Execute_Scalar(ByVal strSQLCommand As String, _
ByVal dctOfParameterVariables As Dictionary(Of String, Object), _
ByVal strConnection As String, _
Optional ByVal strSQLType As String = "S") As Object
Try
Using conApp As New SqlConnection(strConnectio n)
Dim cmdSqlCommand As New SqlCommand
cmdSqlCommand = ERV_Global_Web.Query_Data( dctOfParam eterVariab les)
cmdSqlCommand.Connection = conApp
cmdSqlCommand.CommandText = strSQLCommand
cmdSqlCommand.CommandType = CommandType.Text
conApp.Open()
Execute_Scalar = cmdSqlCommand.ExecuteScala r
If Execute_Scalar Is Nothing Then
Return Return_Empty_Value(strSQLT ype)
End If
End Using
Catch ex As Exception
End Try
Return ""
End Function
Public Shared Function Return_Empty_Value(ByVal strSQLType) As Object
Select Case strSQLType
Case "S"
Return ""
Case "D"
Return System.DBNull.Value
Case "N"
Return 0
End Select
Return Nothing
End Function
Could it be made more robust ?
Thank you.
I have simplified into :
Public Function Get_Member_Name(ByVal strMemberId As String, _
Optional ByVal blnMemberName As Boolean = False) As String
Try
sbdSQLCommand.Length = 0
sbdSQLCommand.AppendLine("
If blnMemberName Then
Else
sbdSQLCommand.AppendLine("
End If
sbdSQLCommand.AppendLine("
sbdSQLCommand.AppendLine("
Dim dic As New Dictionary(Of String, Object)
dic.Add("@MemberId", strMemberId)
Return ERV_Global_Web.Execute_Sca
Catch ex As Exception
End Try
Return ""
End Function
Public Shared Function Execute_Scalar(ByVal strSQLCommand As String, _
ByVal dctOfParameterVariables As Dictionary(Of String, Object), _
ByVal strConnection As String, _
Optional ByVal strSQLType As String = "S") As Object
Try
Using conApp As New SqlConnection(strConnectio
Dim cmdSqlCommand As New SqlCommand
cmdSqlCommand = ERV_Global_Web.Query_Data(
cmdSqlCommand.Connection = conApp
cmdSqlCommand.CommandText = strSQLCommand
cmdSqlCommand.CommandType = CommandType.Text
conApp.Open()
Execute_Scalar = cmdSqlCommand.ExecuteScala
If Execute_Scalar Is Nothing Then
Return Return_Empty_Value(strSQLT
End If
End Using
Catch ex As Exception
End Try
Return ""
End Function
Public Shared Function Return_Empty_Value(ByVal strSQLType) As Object
Select Case strSQLType
Case "S"
Return ""
Case "D"
Return System.DBNull.Value
Case "N"
Return 0
End Select
Return Nothing
End Function
Could it be made more robust ?
Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
In web app.
Regarding the connection string, should we always call a function to get it every time a query is going to counduct?
Thank you.
Regarding the connection string, should we always call a function to get it every time a query is going to counduct?
Thank you.
In a web app I would store it in the connection strings section of web.config.
ASKER
Yes, and just use it ?
Thank you.
Thank you.
Yip
ASKER
Would you please provide simple sample of database access library ?
Thank you.
Thank you.
Unfortunately I can't. But roughly just take what you have and make it generic. A query pretty much needs 2 things, the SQL and the parameters. So create a static class library with the methods you need to access the database e.g. GetValue(String Sql, Dictionary Params), ExecuteSql(String Sql, Dictionary Params), GetRecordSet(String Sql, Dictionary Params).
HTH
HTH
ASKER
Ok. Is there any link that is discuss about it ?
Thank you.
Thank you.
I don't know of any, but seriously its hardly more than you've done, just generic so you can use the same code from 2 pages.
ASKER
Ok then.
Thank you very much for your help.
Thank you very much for your help.
This is pretty useful overview of data access - its old now, but still mostly relevant.
http://msdn.microsoft.com/en-us/library/ee817654.aspx
http://msdn.microsoft.com/en-us/library/ee817654.aspx
ASKER
It is great.
Thank you.
Thank you.