以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- [求助]多表查询 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=125804) |
-- 作者:2900819580 -- 发布时间:2018/10/7 16:17:00 -- [求助]多表查询 如下代码,我是加载了第一个表,再加载列,再遍历行,查找相关的数据,能不能用SQL 中的INNER JOIN语句,或者有没有其它更好的方法, 因为遍历的话,如果行数多的话,会很慢! Dim cmd As new SQLCommand Dim Sqldt1 As DataTable cmd.C cmd.CommandText = "sel ect Top 10 * from {ICInventory} " Sqldt1 = cmd.ExecuteReader Dim tb As Table = Tables(e.Form.Name & "_Table1") Dim dt As DataTable = tb.DataTable tb.DataSource = Sqldt1 Dim lms() As String = {"代码","名称","规格型号"} For Each lm As String In lms If lm = "用量" Then dt.DataCols.Add(lm,Gettype(Single)) Else dt.DataCols.Add(lm,Gettype(String),250) End If Next Dim cmd As new SQLCommand Dim Sqldt As DataTable cmd.C For Each r As Row In tb.Rows cmd.CommandText = "Sel ect * From {t_ICItemCore} Where [FItemID] = \'" & r("FItemID") & "\'" Sqldt = cmd.ExecuteReader If Sqldt.DataRows.Count > 0 Then Dim dr As DataRow = sqldt.DataRows(0) r("代码") = dr("FNumber") r("名称") = dr("FName") r("规格型号") = dr("Fmodel") End If Next |
-- 作者:有点蓝 -- 发布时间:2018/10/7 17:21:00 -- Select Top 10 a.*,b.FNumber As 代码,b.FName As 名称 from {ICInventory} As a Inner join {t_ICItemCore} As b on a.FItemID = b.FItemID |
-- 作者:2900819580 -- 发布时间:2018/10/8 14:56:00 -- 列名 \'FName\' 不明确。 多表出现以上错误 ,以下是代码 cmd.CommandText = "Sele ct a.*,b.FNumber As 代码,b.FName As 名称,b.Fmodel as 规格型号,c.Fnote as 库位号,e.FName as 仓库名称 from {ICInventory} As a " cmd.CommandText &= " Inner join {t_ICItemCore} As b on a.FItemID = b.FItemID " cmd.CommandText &= " Inner join {t_ICItemMaterial} As c on a.FItemID = c.FItemID " cmd.CommandText &= " Inner join {t_ICItemBase} As d on a.FItemID = d.FItemID " cmd.CommandText &= " Inner join {t_ICItemBase} As e on d.FDefaultLoc = e.FItemID " cmd.CommandText &= " WHERE FName LIKE \'%机壳%\'" |
-- 作者:有点甜 -- 发布时间:2018/10/8 15:05:00 -- 条件 WHERE FName LIKE \'%机壳%\' 中的 fname 是哪个表的?也要指定表名的 |
-- 作者:2900819580 -- 发布时间:2018/10/8 15:08:00 -- 以下是引用有点甜在2018/10/8 15:05:00的发言:
条件 WHERE FName LIKE \'%机壳%\' 中的 fname 是哪个表的?也要指定表名的 解决了,cmd.CommandText &= " WHERE E.FName LIKE \'%机壳%\'" 谢谢老师 |
-- 作者:2900819580 -- 发布时间:2018/10/9 15:26:00 -- 老师,如果两个表不在同一个数据库上,怎么办。 ’~~~~~~~~~~~~~~~~~~~~~~~`{t_ICItemCore} Dim cmd As new SQLCommand Dim Sqldt As DataTable cmd.Conn ecti ‘~~~~~~~~~~~~~~~~~~~{物料表} Dim cmd1 As new SQLCommand Dim Sqldt As DataTable cmd1.Conn ecti cmd.CommandText = "Se lect b.FNumber As 代码,b.FName As 名称,b.Fmodel as 规格型号,c.Fnote as 库位号,e.FName as 仓库名称 from {t_ICItemCore} As b " cmd.CommandText &= " Inner join {物料表} As C on c.代码 = b.FNumber " Sqldt = cmd.ExecuteReader [此贴子已经被作者于2018/10/9 15:27:12编辑过]
|
-- 作者:有点甜 -- 发布时间:2018/10/9 15:28:00 -- 如果都是sqlserver数据库,在同一个电脑上,可以加上数据库的名称查询;
如果都是access数据库,同样也可以直接查询。
如果一个sql一个access,或者一个内部一个外部,不能直接写sql语句。 |
-- 作者:2900819580 -- 发布时间:2018/10/9 15:46:00 -- 老师,都是SQL数据库上,也是同一个服务器,只是数据源名称不一样, 代码应该怎么写。 数据源名称没办法上传 Dim cmd As new SQLCommand Dim Sqldt As DataTable ‘cmd.Connec ti Dim cmd1 As new SQLCommand Dim Sqldt As DataTable ’cmd1.Connec ti cmd.CommandText = "Selec t b.FNumber As 代码,b.FName As 名称,b.Fmodel as 规格型号,c.Fnote as 库位号,e.FName as 仓库名称 from {t_ICItemCore} As b " cmd.CommandText &= " Inner join {物料表} As C on c.代码 = b.FNumber " Sqldt = cmd.ExecuteReader [此贴子已经被作者于2018/10/9 15:47:58编辑过]
|
-- 作者:有点甜 -- 发布时间:2018/10/9 15:51:00 -- 1、connectionName随便用任意一个;
2、sql语句的表名,要修改一下,比如之前叫 表A、表B,那就要改成 数据库1.ado.表A,数据库2.ado.表B |
-- 作者:2900819580 -- 发布时间:2018/10/9 16:33:00 -- cmd.CommandText = "Sel ect b.FNumber As 代码,b.FName As 名称,b.Fmodel as 规格型号,c.Fnote as 库位号,e.FName as 仓库名称 from {cmd.ado.t_ICItemCore} As b " cmd.CommandText &= " Inner join {cmd1.ado.物料表} As C on c.代码 = b.FNumber " 对象名 \'cmd.ado.t_ICItemCore\' 无效。 |