Formatting string function

  • Thanks guys.. I appreciate it very much..

    I made a bunch of functions already where I insert, delete, update, check if record exists and it's comfortable, I just call them from any place. Now I have to rewrite it again with parameterized queries It's a little bit of a challenge because I have to pass table name, arrays of fields, arrays of values, array of datatypes, and then figure out how to add them to the parameter command..

    Function

    deleteRecord(ByVal tableName As String, ByVal fields As Array, ByVal Values As Array, ByVal datatypes As Array) As String

    For count = 0 To (fields.Length - 1)

       command.Parameters.Add("@" & fields(count), SqlDbType. ??? how to add here datatypes(count)

      command.Parameters.("@" & field(count)).Value = values(count)

    Next count

     

  • you are doing some weird stuff there.  you would probably be better off putting this logic in sprocs rather than VB. 

    anyway, on to your question: have a look at the Enum.Parse() method if you need to create an enum value from a string.  is that what you are asking for?

    http://msdn2.microsoft.com/en-us/library/system.enum.parse.aspx

    ---------------------------------------
    elsasoft.org

  • In .NET you just have to run searches in MSDN2 for code samples for what you need since you are using ADO.NET, try the link below for details.  Hope this helps.

    http://aspnet101.com/aspnet101/tutorials.aspx?id=1

    http://www.codeproject.com/aspnet/ObjectDataSourceInDepth.asp

     

    Kind regards,
    Gift Peddie

  • This is a type of function I was talking about. I just made it. I don't want to make anybody bored here with my kitty scripting, just thought maybe it can be useful for some other beginner like me. I didn't test it yet, maybe it doesn't work. Wrote it on a piece of paper with a pencil .

    Peddie, say I am a good girl  tonight !

    Function insertRecordParam(ByVal tableName As String, ByVal fields As Array, ByVal values As Array) As String

    Dim result As String = ""

    Dim length As Integer

    Dim count As Integer

    Dim queryString As String = "Insert into " & tableName & " ("

    Dim placeHolder As String = " values ("

    For count = 0 To (fields.Length - 1)

        queryString &= fields(count) &

    ","

        placeHolder &=

    "@" & fields(count) & ","

    Next count

    length = queryString.Length

    queryString = queryString.Substring(0, length - 1)

    queryString &=

    ") "

    length = placeHolder.Length

    placeHolder = placeHolder.Substring(0, length - 1)

    placeHolder &=

    ")"

    queryString &= placeHolder

     

    Dim connection As New Data.SqlClient.SqlConnection

    connection = getDBConnection()

    Try

        Dim query As New Data.SqlClient.SqlCommand(queryString, connection)

        For count = 0 To (fields.Length - 1)

            query.Parameters.AddWithValue(

    "@" & fields(count), values(count))

        Next count

        connection.Open()

        If query.ExecuteNonQuery Then

            result =

    "Thank you! Record has been added to the Database."

        Else

            result =

    "Couldn't insert a record."

        End If

        Catch ex As Exception

            result =

    "Couldn't access the database due to this error: " & ex.Message

    Finally

            connection.Close()

    End Try

    Return result

    End Function

     

    Good night ...

Viewing 4 posts - 16 through 18 (of 18 total)

You must be logged in to reply to this topic. Login to reply