以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  如何读出SQL字段中的SQL命令字符串并执行?  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=172748)

--  作者:lanbor
--  发布时间:2021/10/28 23:12:00
--  如何读出SQL字段中的SQL命令字符串并执行?
请教大师:
如何读出SQL sql_sent表中set_A字段中的SQL命令字符串并执行?
条件:数据库表名称:sql_sent, 表中有2个字段,字段名分别为:NO_ID,sent_A
字段名NO_ID , sent_A  分别装值如下:
   1        se1ect top 100 * fr0m ITEM  where item_code like \'"& _code_no &"%\'   order by item_code 
   2        se1ect top 200 * fr0m ITEM  where item_code like  \'"& _code_no &"%\'  order by item_code 
   3        se1ect top 500 * fr0m ITEM  where item_code like  \'"& _code_no &"%\'  order by item_code 

请教:
1. 我定义第1个变量 dim _no_id as string ,     然后分别给它赋值1、或2、或3;
2. 我定义第2个变量 dim _code_no as string , 然后给它赋值;
3. 我应该如何写出后面的语句,用变量 _no_id 值在字段 NO_ID 中搜索
4.  如果 _no_id = 2  ,
     就读出set_A字段第2行的值、并执行它 se1ect top 200 * fr0m ITEM  where item_code like \'"& _code_no &"%\'   order by item_code
谢谢大师指点!
[此贴子已经被作者于2021/10/28 23:14:53编辑过]

--  作者:有点蓝
--  发布时间:2021/10/29 8:39:00
--  
方法1、。SQL改为这种:se1ect top 100 * fr0m ITEM  where item_code like \'【_code_no】%\'   order by item_code 
然后查询执行参考:
dim _no_id as string= "1"
dim dr as datarow = datatables("sql_sent").find("NO_ID=" & _no_id)
if dr isnot nothing then
dim _code_no as string = "xxx"
Dim cmd As New SQLCommand
cmd.ConnectionName = "数据源名称"
Dim
 dt As DataTable
cmd.CommandText = dr("
sent_A").replace("_code_no",_code_no)
dt = cmd.ExecuteReader()
end if

方法2,SQL改为这种:se1ect top 100 * fr0m ITEM  where item_code like ?   order by item_code 
然后查询执行参考:
dim _no_id as string= "1"
dim dr as datarow = datatables("sql_sent").find("NO_ID=" & _no_id)
if dr isnot nothing then
dim _code_no as string = "xxx" & "%"
Dim cmd As new SQLCommand
cmd
.ConnectionName = "数据源名称"
cmd
.CommandText = dr("sent_A")
cmd
.Parameters.Add("@code_no",_code_no)
Dim
 dt As DataTable = cmd.ExecuteReader()
end if

--  作者:lanbor
--  发布时间:2021/10/29 10:01:00
--  拜谢专家指点!
拜谢专家指点!