如何将一列SQL查询用于C#代码中的另一个SQL查询 [英] How to use one column of SQL query into another SQL query inside C# code

查看:81
本文介绍了如何将一列SQL查询用于C#代码中的另一个SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在下面的c#代码中使用query1列显示组到查询2中。我已经指出了我想把query1列放在????的地方符号。



How to use query1 column Display Group into query 2 in the below c# code. I have denoted the place where i want to put query1 column by ???? symbol.

[Test]
    [Category(TestType.NeedsDeployment)]
    public void PopulateRangeInStores()
    {
        ExecutePopulateRangeInStoreProcedure("csg_sp_populate_RangeInStore");

        using (var connection = IKBDatabaseConnection.GetConnectionForIKBTFS())
        {
            string query1 = SELECT count (distinct DESC7) FROM ix_spc_planogram (NOLOCK) WHERE dbstatus= 1";
            string query2 = SELECT count (distinct EquipmentType) FROM Csg_Range_In_Store (NOLOCK) WHERE DisplayGroup = '" + ?????+ "'";
            var command1 = new SqlCommand(query1, connection);
            var command2 = new SqlCommand(query2, connection);
            //string output = " ";
            //var  = " ";
            //var actualDG = " ";
            var actualDG = " ";
            var expectedDG = " ";
            var dataReader1 = command1.ExecuteReader();
            var dataReader2 = command2.ExecuteReader();
            if (dataReader1.Read())
            {
                DataTable dt = new DataTable();
                dt.Load(dataReader1);
                expectedDG = dt.Rows.Count.ToString();
            }

            if (dataReader2.Read())
            {
                DataTable dt = new DataTable();
                dt.Load(dataReader2);
                actualDG = dt.Rows.Count.ToString();
            }
            actualDG.Should().Be(expectedDG);



        }

    }


    private void ExecutePopulateRangeInStoreProcedure(string storedProcedure)
    {
        using (var connection = IKBDatabaseConnection.GetConnectionForIKBTFS())
        {
            using (SqlCommand cmd = new SqlCommand(storedProcedure, connection))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@ix_sys_error", SqlDbType.Int).Value = 0;

                cmd.CommandTimeout = 0;
                cmd.ExecuteNonQuery();
            }
        }
    }





我尝试了什么:



我不确定我们是否可以在c#code中使用查询1列进入查询2



What I have tried:

I am not sure whether we can use query 1 column into query 2 in c# code

推荐答案

朋友,

请使用此功能。希望这对你有所帮助。



string query1 = SELECT count(distinct DESC7)FROM ix_spc_planogram(NOLOCK)WHERE dbstatus = 1;

string query2 = SELECT count( distinctType)FROM Csg_Range_In_Store(NOLOCK)WHERE DisplayGroup IN('+ query1 +');



如果帮助回复必须谢谢......
Hi Friend,
Please use this. hope this will help to you.

string query1 = SELECT count (distinct DESC7) FROM ix_spc_planogram (NOLOCK) WHERE dbstatus= 1";
string query2 = SELECT count (distinct EquipmentType) FROM Csg_Range_In_Store (NOLOCK) WHERE DisplayGroup IN ('"+ query1+ "')";

If help reply must Thanks...


这篇关于如何将一列SQL查询用于C#代码中的另一个SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆