SQL:.NET 与 SSMS

SQL : .NET vs SSMS

Solved!

As it turns out, the culprit was the query used in Buyer table. The data came from a recursive common table expression by splitting comma separated string and then re-assemble them into rows. As I dig further into this issue, it was returning the first string of the common spliced value and use that as a comparison with the supplier table, thus making other variables unavailable.

The way I fixed it was save the result to a temp table, and then reference it after the recursive was finished (In this case I just changed it to a update code).

SSMS will handle this correctly since it waits for the code to finish the CTE first before being reference in another set of code, while the WPF uses the local driver on the client machine. The driver did not handle the case for recursive CTE, and thus returned the first pass of the code for the comparison.

Thank you for your help and response. It gave me some insight to where I need to break down the code and further analysis where it was not working and where it was working.

我已经为这个问题苦苦挣扎了两天了。我已经重写了代码和语句,但结果还是一样。

我的目标是能够从供应商列表中获取全名,其中具有来自不同 table 的相似买家名称。我使用的代码:

SELECT BuyerID, SupName 
FROM Buyers b 
LEFT OUTER JOIN suppliers s ON s.supname LIKE RTRIM(b.BuyerName) + '%'

我也试过这段代码

SELECT b.BuyerID, b.BuyerName, s.SupName 
FROM Buyers b, suppliers s 
WHERE PATINDEX(RTRIM(b.BuyerName) + '%', s.supname) <> 0

我的预期输出应如下所示,它与 Microsoft SQL Server Management Suite 产生相同的结果,如下所示。

BuyerID | BuyerName | Supname
--------+-----------+---------------------
AA      | Alfrac    | Alfrac Insurance.
BC      | Bank      | Bank of America
CC      | Charle    | Charles Dawnson

我的WPF输出结果是这样的。

BuyerID | BuyerName | Supname
--------+-----------+---------------------
AA      | Alfrac    | Null
BC      | Bank      | Null
CC      | Charle    | Charles Dawnson

在 WPF 中,这是我使用的代码:

string sql = ReadEmbeddedResource("Queries.GetBuyerSupplier.sql");
DataSet Data = new DataSet();
using (SqlCommand cmd = new SqlCommand(sql, new SqlConnection(ConfigurationManager.ConnectionStrings["sqldb"].ConnectionString)))
{
    cmd.Connection.Open();
    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
        da.Fill(Data);
    cmd.Connection.Close();
}

在Queries.GetBuyerSupplier.sql里面就是上面的SQL命令。

这是我尝试过的事情的列表:

  1. Google
  2. 开启 ARITHABORT
  3. 选项(重新编译)
  4. 常用Table表达式
  5. 声明并使用 Temp Table
  6. 更改登录凭据(使用 SQL 与域身份验证)

我使用的工具版本:

Microsoft SQL Server 2014 (SP2-GDR) (KB4019093) - 12.0.5207.0 (X64) 2017 年 7 月 3 日 02:25:44 Microsoft Corporation 标准版(64 位)在 Windows NT 6.3(内部版本 9600:)(管理程序)

SQL 客户端驱动程序: Microsoft SQL Server 2008 R2 Native Client v 10.50.1617.0

Microsoft Visual StudioProfessional 2015 V 14.0.25431.01 Update 3
Microsoft .NET Framework V 4.7.02558 OS: Windows 7 64 位。

已解决。事实证明,另一组代码是 CTE 的递归(最初代码被送入逗号拼接,然后我有一个 CTE 递归将字符串拆分成行,然后送入上面的代码。一旦我将 CTE 递归结果设置为临时 table,我终于返回并使用正确的供应商更新 table,现在它们似乎都在正常工作。

我解决此问题的方法是首先将买家代码附加到临时 table,递归完成后,我随后使用 LEFT OUTER JOIN 比较对 supname 进行更新。

只是想知道为什么我的驱动程序在比较结果之前不让递归完成?那里似乎有点破,看看它如何只 return 返回许多未完成结果中的一个。在 returning 结果之前,SSMS 做了正确的等待递归完成的工作。

目前是驱动程序问题。谢谢你们的帮助。