-1

I have a project without any parameters used in SQL queries. Is there any solution so that i don't have to change the function and validate parameters from the Query string itself?

Query = "select * from tbl_Users where userName='"& textbox1.text &"' and password='"& textbox2.text &"' "
ds = obj.ExecuteQueryReturnDS(Query)

Function where query is passed:

Public Function ExecuteQueryReturnDS(ByVal stQuery As String) As DataSet
        Try
            Dim ds As New DataSet
            Using sqlCon As New SqlConnection(connStr)
                Dim sqlCmd As New SqlCommand(stQuery, sqlCon)
                Dim sqlAda As New SqlDataAdapter(sqlCmd)
                sqlCmd.CommandType = CommandType.Text
                sqlAda.Fill(ds)
            End Using
            Return ds
        Catch ex As Exception

        End Try
    End Function

I tried passing parameters into the function but the function is used in for other queries as well hence i cannot define the parameters inside the function .

Is there any work around

Tharif
  • 13,794
  • 9
  • 55
  • 77

2 Answers2

3

I think the only solution is create a new function and gradually migrate to it.

Public Function ExecuteQueryReturnDS(ByVal cmdQuery As SqlCommand) As DataSet

    Try
        Dim ds As New DataSet
        Using sqlCon As New SqlConnection(connStr)
            cmdQuery.Connection = sqlCon
            Dim sqlAda As New SqlDataAdapter(cmdQuery)
            sqlAda.Fill(ds)
        End Using
        Return ds
    Catch ex As Exception
    End Try

End Function

cmdQuery is intended to be an SqlCommand to which you already added all the parameters you need.

tezzo
  • 10,858
  • 1
  • 25
  • 48
1

As an intermediate step before switching to a full parameterized application you could change your actual method to be able to receive an optional argument. This optional argument will be your SqlParameter array defined in the point where you call this query

Public Function ExecuteQueryReturnDS(ByVal stQuery As String, Optional ByVal prms As SqlParameter() = Nothing) As DataSet
   Try
       Dim ds As New DataSet
       Using sqlCon As New SqlConnection(connStr)
           Dim sqlCmd As New SqlCommand(stQuery, sqlCon)
           if Not prms Is Nothing Then
               sqlCmd.Parameters.AddRange(prms)
           End if
           Dim sqlAda As New SqlDataAdapter(sqlCmd)
           sqlCmd.CommandType = CommandType.Text
           sqlAda.Fill(ds)
       End Using
       Return ds
   Catch ex As Exception

   End Try
End Function
Steve
  • 213,761
  • 22
  • 232
  • 286