Can I use C # SqlParameter

I make a continuous selection every 10 seconds, so I thought that I would do some premature optimizations and keep creating cmd and paramater objects in everyloop

if I do it in one way

public void FirstSelect() {

    // select data
    this.cmdSelectData = new SqlCommand(SQL_SELECT_DATA, conn);

    this.paramBranchId = new SqlParameter("@branch_id", 1);
    this.cmdSelectData.Parameters.Add(paramBranchId);

    // fetch data blah, blah, blah...
}

and then this in another method

public void SecondSelect() {

    this.paramBranchId.Value = 2;
   // fetch data
}

will work as expected, one select using branch 1, one select using branch 2 or I need

this.cmdSelectData.Parameters.Clear();
ths.cmdSelectData.Parameters.Add(new SqlParameter( // for branch 2)

}

+3
source share
2 answers

Yes, setting the value of an existing parameter affects the subsequent actions of the command. This is very convenient if you want to execute the same command several times with different values ​​for one or more parameters, without having to rebuild it all every time.

+5

.

+1

Source: https://habr.com/ru/post/1712259/


All Articles