【问题标题】:Executing SQL command works in SQL but not in .C#执行 SQL 命令在 SQL 中有效,但在 .C# 中无效
【发布时间】:2017-08-17 17:57:21
【问题描述】:

我有一个相当大的 SQL 查询,当我在 MS SQL 中执行它时,它可以按预期工作。但是,当我从 C# 运行它时,它不会获取任何行(dataTable 不会返回任何行)。如果我改为使用简单的查询,例如C# 中的“SELECT * Accounts FROM TableX”,dataTable 返回它应该做的所有行。我还尝试从 SQL 查询中删除所有空格,以便所有内容都在同一行,但没有任何更改。代码如下:

internal void GetData()
    {
        try
        {
            using (SqlConnection connection = new SqlConnection(connectionBuilder.ToString())) //1. Open connection
            {
                connection.Open();
                using (SqlDataAdapter dataAdapter = new SqlDataAdapter(GetSqlString(), connection))
                {
                    DataTable dataTable = new DataTable();
                    dataAdapter.Fill(dataTable);

                    foreach (DataRow dataRow in dataTable.Rows)
                    {
                        var field1 = dataRow[0];
                        var field2 = dataRow[1];
                        Logger.Log(field1 + " " + field2);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            utilityProvider.Log("Error" + ex.ToString());
        }
    }

    private string GetSqlString()
    {

        return @"SELECT 
        Field1 = subscr.cind_recipient,

        Field2 = COALESCE(
                a.name, s.cind_name, 
                    CASE WHEN c.cind_is_protected_identity = 0 THEN c.fullname 
                    ELSE ISNULL(c.cind_protected_firstname, '') + ' ' + ISNULL(c.cind_protected_lastname, '') END ), 

        Field3 = COALESCE(CASE WHEN c.cind_is_protected_identity <> 1 THEN c.address1_line3 
            ELSE c.cind_protected_address1_line3 END,
                 s.cind_postal_line3),

        Field4 = COALESCE(CASE WHEN c.cind_is_protected_identity = 0 THEN c.address1_line1 
            ELSE c.cind_protected_address1_line1 END,
                               a.address2_line1, s.cind_postal_line1),

        Field5 = COALESCE(CASE WHEN c.cind_is_protected_identity = 0 THEN c.address1_line2 
            ELSE c.cind_protected_address1_line2 END, 
                               a.address2_line2, s.cind_postal_line2),

        Field6 = COALESCE(CASE WHEN c.cind_is_protected_identity = 0 THEN mpc1.cind_postalcode 
            ELSE mpc.cind_postalcode END,
                            a.address1_postalcode, s.cind_postal_postalcode),

        Field7 = COALESCE(CASE WHEN c.cind_is_protected_identity = 0 THEN mpc1.cind_city 
            ELSE mpc.cind_city END, 
                          a.address1_city, s.cind_postal_city),

        Field8 = c.cind_member_number,

        Field9 = ISNULL(COALESCE(c.cind_union_section_idname, a.cind_mml_mub_union_section_idname), a1.cind_mml_mub_union_section_idname),

        Field10 = CASE WHEN c.cind_is_protected_identity <> 1 THEN c.cind_soc_sec_no 
            ELSE c.cind_protected_cind_soc_sec_no END,

        Field11 = COALESCE(a.cind_organization_no, s.cind_organization_no),
        Field12 = c.gendercodename,
        Field13 = cind_count,
        Field14 = k1.cind_name,
        Field15 = k1.cind_number,
        Field16 = k2.cind_name,
        Field17 = k2.cind_number,
        Field18 = 'sam',
        Field19 = subscr.villa_free_exname

        FROM dbo.Filteredcind_mml_mub_subscription subscr

        INNER JOIN Filteredcind_mml_mub_service svc
            ON subscr.cind_mml_mub_service_id = svc.cind_mml_mub_serviceid
                AND svc.cind_code = 'PE002'

        LEFT JOIN Filteredcind_mml_mub_site s
            ON subscr.cind_mml_mub_site_id = s.cind_mml_mub_siteid

        LEFT JOIN FilteredAccount a
            ON subscr.cind_account_id = a.accountid

        LEFT JOIN  FilteredAccount a1 
            ON a1.accountid = s.cind_account_id

        INNER JOIN FilteredContact c
            ON subscr.cind_contact_id = c.contactid
                AND c.statecode = 0

        LEFT JOIN Filteredcind_mml_mub_postalcity mpc
            ON c.cind_protected_cind_postalcity_id = mpc.cind_mml_mub_postalcityid

        LEFT JOIN Filteredcind_mml_mub_postalcity mpc1
            ON c.cind_postalcity_id = mpc1.cind_mml_mub_postalcityid

        LEFT JOIN Filteredcind_mml_mub_county lan
            ON  c.cind_county_id = lan.cind_mml_mub_countyid

        LEFT JOIN Filteredcind_mml_mub_country land
            ON lan.cind_country_id = land.cind_mml_mub_countryid

        LEFT JOIN (Filteredcind_mml_mub_membership m1
            INNER JOIN (SELECT mt1.cind_mml_mub_membership_typeid
                        FROM Filteredcind_mml_mub_membership_type mt1
                        WHERE mt1.cind_code = 'PRDI-45') mtt1
                        ON mtt1.cind_mml_mub_membership_typeid = m1.cind_mml_mub_membership_type_id)
            ON c.contactid = m1.cind_contact_id
            AND (m1.statuscode = 1
                OR m1.statuscode = 434578)
            AND m1.statecode = 0

        LEFT JOIN Filteredcind_mml_mub_local_union k1
            ON m1.cind_mml_mub_local_union_id = k1.cind_mml_mub_local_unionid

        LEFT JOIN (Filteredcind_mml_mub_membership m2
            INNER JOIN (SELECT mt2.cind_mml_mub_membership_typeid
                        FROM Filteredcind_mml_mub_membership_type mt2
                        WHERE mt2.cind_code = 'EXTR-01') mtt2
                        ON mtt2.cind_mml_mub_membership_typeid = m2.cind_mml_mub_membership_type_id)
            ON c.contactid = m2.cind_contact_id
            AND (m2.statuscode = 1
                OR m2.statuscode = 126670001)
            AND m2.statecode = 0

        LEFT JOIN Filteredcind_mml_mub_local_union k2
            ON m2.cind_mml_mub_local_union_id = k2.cind_mml_mub_local_unionid

        WHERE subscr.statuscode = 1
            AND subscr.statecode = 0";
    }

【问题讨论】:

  • 可能是超时问题。见stackoverflow.com/questions/30910016/…
  • 使用Sql Profiler 连接到Sql Server 实例。观察发送到服务器的查询并从那里复制它。现在将其粘贴到您的 SSMS 中并重新执行。如果结果相同(没有返回结果),那么它是查询本身的问题。如果结果不同,那么您可能没有连接到同一个实例或不同的架构。
  • 您是否尝试在 SSMS 中再次执行探查器查询?输出是什么?
  • 好的。我今晚或明天早上试试。
  • @Igor。感谢您的回复。我按照您的描述尝试了 Sql profiler,但是那里显示的查询在 MS SQL 中运行没有任何问题。我使用相同的架构。

标签: c# sql-server datatable sqldataadapter


【解决方案1】:

如果SELECT * 有效,则可能是超时问题。但是,您应该收到一个例外。

默认超时为 30 秒。

您的查询在 MSSQL 中运行需要多长时间?

通过设置改变超时时间

cmd.Timeout = 300; // Change the number of seconds. 

或者提高查询的效率。

【讨论】:

  • 我认为问题不在于超时,因为他没有遇到超时异常。我想,他甚至没有运行它的查询。
  • @Mr.AF 正确。
  • @CathalMF 谢谢。但这并没有什么不同。
  • @kranis100 那么你的问题一定是 SQL 查询,它不能和你在 mssql 中运行的一样,因为你的其余代码都很好。
  • @CathalMF 我尝试使用 SQL 探查器,数据库收到的查询是相同的。
【解决方案2】:

逐行减少你的SQL然后你就会发现问题。从删除所有列开始,使用 * 代替(如 CathalMF 所说),然后一一删除连接。

【讨论】:

    猜你喜欢
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    相关资源
    最近更新 更多