SQL statements are primarily manipulated for conditional additions, deletions, modifications, or queries.
However, in a variety of variable query conditions, the length of the SQL statement is not necessarily, it will require our SQL statement stitching.
Like what:
String sqlstr= "Select condition 1, Condition 2, Condition 3, Condition 4 from table 1 S1 INNER JOIN table 2 where";
When making multiple table queries in a program, because the length of the SQL statement is variable, we need string concatenation.
Sqlstr + = "(S1. s_id like @query or S1. S_name like @query or S1. S_mobile like @query or S1. S_address like @query or S1. S_crad like @query) and ";
Because I don't know how many SQL statements to splice, so
Add and after each SQL statement.
SqlParameter name = new SqlParameter ("@query", "%" + TextBox2.Text + "%");
Comm. Parameters.Add (name);
And then splicing the next SQL statement.
At the end of the final spliced SQL statement, write:
Comm.commandtext = Sqlstr. TrimEnd (new char[] {' A ', ' n ', ' d ', '});
Because TrimEnd is a trailing match for a set of characters specified in the array from the current System.String object,
So, we need to define an array new char[]{' a ', ' n ', ' d '} on top.
If you do not remove and, the program will error.
SQL statement stitching in SQL Server