执行SQL命令在SQL中工作但在.C#中不起作用

时间:2017-03-24 17:20:04

标签: c# sql-server datatable sqldataadapter

我有一个非常大的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";
    }

2 个答案:

答案 0 :(得分:0)

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

默认超时为30秒。

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

通过设置

更改超时
cmd.Timeout = 300; // Change the number of seconds. 

或者提高查询效率。

答案 1 :(得分:0)

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