Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

Financialscmd = New OleDbCommand("insert into StrgOper (TransID, StrgOperItem,…

  1. User

    Financialscmd = New OleDbCommand("insert into StrgOper (TransID, StrgOperItem, StrgOperQty, StrgOperTime, StrgOperUser) Values (" & TransID.Text & ", '" & DataGridView1(0, DataGridView1.Rows(i).Index).Value & "', " & Val(DataGridView1(1, DataGridView1.Rows(i).Index).Value) & ",#" & TransTime.Value.ToString("MM-dd-yyyy HH:mm:ss") & "#,'" & TransUser.Text & "')", con) If con.State = ConnectionState.Closed Then con.Open() End If Financialscmd.ExecuteNonQuery() con.Close() I want to make it parametriezed

  2. Assistant

    To make the code parameterized, you can modify it as follows:

    Dim Financialscmd As New OleDbCommand("insert into StrgOper (TransID, StrgOperItem, StrgOperQty, StrgOperTime, StrgOperUser) Values (@TransID, @StrgOperItem, @StrgOperQty, @StrgOperTime, @StrgOperUser)", con)
    Financialscmd.Parameters.AddWithValue("@TransID", TransID.Text)
    Financialscmd.Parameters.AddWithValue("@StrgOperItem", DataGridView1(0, DataGridView1.Rows(i).Index).Value.ToString())
    Financialscmd.Parameters.AddWithValue("@StrgOperQty", Val(DataGridView1(1, DataGridView1.Rows(i).Index).Value))
    Financialscmd.Parameters.AddWithValue("@StrgOperTime", TransTime.Value)
    Financialscmd.Parameters.AddWithValue("@StrgOperUser", TransUser.Text)
    
    If con.State = ConnectionState.Closed Then
        con.Open()
    End If
    
    Financialscmd.ExecuteNonQuery()
    con.Close()
    

    In this code, the SQL query remains the same but the parameters are added using Parameters.AddWithValue() method. The @ symbol is used to indicate the parameter names in the query. The values are then assigned to the parameters using the corresponding properties (Text, Value, etc.) of the form controls.

    This approach helps to prevent SQL injection and improves the performance by allowing the database engine to reuse the query execution plan.