Two ways to include variables in where query conditions in sql statements

      If your query conditions contain string variables rqand you need to embed their values ​​directly into the query statement, you can use string interpolation or string concatenation to build the query statement. Below is the sample code

1. The method of using string interpolation: '{variable}'

string rq = "123";
string query = $"SELECT tb_MM_JD_00001.ID FROM tb_MM_JD_00001 WHERE tb_MM_JD_00001.ID NOT IN (SELECT tb_MM_JD_00004.Source ID FROM tb_MM_JD_00004 WHERE tb_MM_JD _00004.Type='Storage Registration' AND tb_MM_JD_00004.Login Name='admin ') AND CONVERT(NVARCHAR(10), tb_MM_JD_00001.Compilation date, 23) = '{rq}'";

// Execute the query and get the data
// ...

 

2. Use the method of string connection: '" + variable + "'

string rq = "123";
string query = "SELECT tb_MM_JD_00001.ID FROM tb_MM_JD_00001 WHERE tb_MM_JD_00001.ID NOT IN (SELECT tb_MM_JD_00004.Source ID FROM tb_MM_JD_00004 WHERE tb_MM_JD_ 00004.Type='Registration' AND tb_MM_JD_00004.Login name='admin' ) AND CONVERT(NVARCHAR(10), tb_MM_JD_00001.Compilation date, 23) = '" + rq + "'";

// Execute the query and get the data
// ...

 

Guess you like

Origin blog.csdn.net/chentiebo/article/details/131260434
Recommended