如何运行SQL一一并发查询

How to run SQL query concurrently in one by one

我正在开发一个页面,例如 Daily SMS Log,用于在后台检索有关 SMS 运行 的信息。我有这个 SQL 代码:

First Section
    --SELECT    ROW_NUMBER() OVER (ORDER BY RowId DESC)AS RowNumber,p.CampaignName,
    --                              p.MobileNo,
    --                              p.Message,
    --                              p.CharCount,
    --                              p.strSenderID as Sender,
    --                              u.strUserName as UserId,
    --                              ds.strDR as DeliveryStatus,
    --                              ds.strDiscription as Original_DRStatus,
    --                              m.strMessageType as MessageType,
    --                              CONVERT(varchar(20) , p.ReceiveTime) as ReceiveTime,
    --                              CONVERT(varchar(20) , p.SendTime) as SendTime,
    --                              CONVERT(varchar(20) , p.DoneTime) as DoneTime,
    --                              p.RootId as Root,
    --                              sp.ProviderName,
    --                              (CASE intAccountType WHEN 1  THEN 'Promotional' WHEN 2 THEN 'Transactional' WHEN 3 THEN 'OptIn' END)  as AccountType
    --                  INTO        #Results3
    --                  FROM        NEWSMSLOG_2019_01..LOG_010119  p  
    --                  INNER JOIN  deliverstatus ds ON p.DeliveryStatus  = ds.intDR_status inner join users u on u.id = p.userid
    --                  left join senderids b on b.id = p.senderid
    --                  left join messagetype m on m.intcode = p.messagetype
    --                  left join smppproviders sp on sp.RootId=p.RootId
    --                  where 1=1

Second Section
    --                      SELECT      *
    --                      FROM        #Results3

    --                      SELECT 

    --                      CampaignName,MobileNo,Message,CharCount,Sender,UserId,DeliveryStatus,
    --                      Original_DRStatus,MessageType,ReceiveTime,SendTime,DoneTime,Root,ProviderName,AccountType

    --                      FROM        #Results3
    --                      WHERE       RowNumber BETWEEN('1' -1) * '109299' + 1 AND((('1' -1) * '109299' + 1) + '109299') - 1

在这里,当我取消注释第一行到 where 1=1 时,行检索成功。在那之后评论那些行我取消评论第二部分然后结果显示一堆行。现在它在 Management Studio 中运行良好。 现在我只想将此结果显示为网格视图,这是我的代码:

protected void Get_Data()
{
    try
    {
        string str = "";
        DataTable dt = new DataTable();
        str = "SELECT ROW_NUMBER() OVER (ORDER BY RowId DESC)AS RowNumber,p.CampaignName,";
        str += "p.MobileNo,";
        str += "p.Message,";
        str += "p.CharCount,";
        str += "p.strSenderID as Sender,";
        str += "u.strUserName as UserId,";
        str += "ds.strDR as DeliveryStatus,";
        str += "ds.strDiscription as Original_DRStatus,";
        str += "m.strMessageType as MessageType,";
        str += "CONVERT(varchar(20), p.ReceiveTime) as ReceiveTime,";
        str += "CONVERT(varchar(20), p.SendTime) as SendTime,";
        str += "CONVERT(varchar(20), p.DoneTime) as DoneTime,";
        str += "p.RootId as Root,";
        str += "sp.ProviderName,";
        str += "(CASE intAccountType WHEN 1  THEN 'Promotional' WHEN 2 THEN 'Transactional' WHEN 3 THEN 'OptIn' END)  as AccountType";
        str += " INTO #Results3 ";
        str += " FROM NEWSMSLOG_2019_01..LOG_010119  p ";
        str += " INNER JOIN deliverstatus ds ON p.DeliveryStatus = ds.intDR_status inner join users u on u.id = p.userid";
        str += " left join senderids b on b.id = p.senderid";
        str += " left join messagetype m on m.intcode = p.messagetype";
        str += " left join smppproviders sp on sp.RootId = p.RootId";
        str += " where 1 = 1 ";

        if(ddl_users.SelectedItem.Text.ToString() == "All")
            {
                str += string.Empty;
            }
            else
            {
                str += " and p.userid = ' + CONVERT(varchar(5),"+ Convert.ToInt32(ddl_users.SelectedValue.ToString())+") + '";
            }
            if (ddl_sender.SelectedItem.Text.ToString() == "All")
            {
                str += string.Empty;
            }
            else
            {
                str += " and p.Senderid = '+CONVERT(varchar(10),"+Convert.ToInt32(ddl_sender.SelectedValue.ToString())+")+'";
            }
            if(!string.IsNullOrEmpty(txt_mobileno.Text.Trim()))
            {
                str += " and p.MobileNo like '' % '"+txt_mobileno.Text.Trim()+"' % ''";
            }
            else
            {
                str += string.Empty;
            }
            if(ddl_delevery.SelectedItem.Text.ToString() =="All")
            {
                str += string.Empty;
            }
            else
            {
                str += " and p.Deliverystatus in ('+CONVERT(varchar(10),"+Convert.ToInt32(ddl_sender.SelectedValue.ToString())+")+')'";
            }
            if(!string.IsNullOrEmpty(txt_CompaignName.Text.Trim()))
            {
                str += " and p.CampaignName like ''%'"+txt_CompaignName.Text.Trim()+"'%'' '";
            }
            else
            {
                str += string.Empty;
            }
            if(ddl_account.SelectedItem.Text.ToString() == "All")
            {
                str += string.Empty;
            }
            else
            {
                str += " and p.accounttype = '+CONVERT(varchar(2),"+Convert.ToInt32(ddl_account.SelectedValue.ToString())+")+'";
            }
            obj.Execute_Query(str);
            string str1 = " SELECT * FROM #Results3";
            str1 += " SELECT";
            str1 += " CampaignName,MobileNo,Message,CharCount,Sender,UserId,DeliveryStatus,";
            str1 += "Original_DRStatus,MessageType,ReceiveTime,SendTime,DoneTime,Root,ProviderName,AccountType";
            str1 += " FROM #Results3";
            str1 += " WHERE RowNumber BETWEEN('1' - 1) * '500' + 1 AND((('1' - 1) * '500' + 1) + '500') - 1";
            str1 += " DROP TABLE #Results3";
            dt = obj.Get_Data_Table_From_Str(str1);
            ViewState["data"] = dt;
            egrd.DataSource = dt;
            egrd.DataBind();
            ViewState["data"] = dt;

        }
        catch (Exception ex)
        {
            CommonLogic.SendMailOnError(ex);
        }
    }

但我在网格视图中找不到任何记录。这是什么问题?

------------------------已更新------------ ------------------------------

namespace BulkSMSSystem.App_Code.DAL
{
    public class DAL_General : DataAccess
    {
        public DataTable Get_Data_Table_From_Str(string str, string cnn_type = "NEWBULKSMS")
        {
            DataTable dt = new DataTable();
            GetConnection(cnn_type);
            dt = GetDataTableByQuery(str);
            return dt;
        }

        public void Execute_Query(string str, string cnn_type = "NEWBULKSMS")
        {
            GetConnection(cnn_type);
            GetExecuteNonQueryByStr(str);
        }

        public object Execute_Scalar(string str, string cnn_type = "NEWBULKSMS")
        {
            GetConnection(cnn_type);
            object rtn = GetScalarOfStr(str);
            return rtn;
        }

------------------------更新2------------ --------------------------

 public DataTable GetDataTableByQuery(string str_query)
        {
            try
            {
                mobj_SqlCommand.CommandText = str_query;
                mobj_SqlCommand.CommandTimeout = mint_CommandTimeout;
                mobj_SqlCommand.CommandType = CommandType.Text;

                //mobj_SqlConnection.Open();
                SqlDataAdapter adpt = new SqlDataAdapter(mobj_SqlCommand);
                DataTable ds = new DataTable();
                adpt.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                CloseConnection();
            }
        }

 public void GetExecuteNonQueryByStr(string query_str)
        {
            try
            {
                mobj_SqlCommand.CommandType = CommandType.Text;
                mobj_SqlCommand.CommandText = query_str;
                mobj_SqlCommand.Connection = mobj_SqlConnection;
                mobj_SqlCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
                CloseConnection();
            }
        }

------------------------更新3------------ ----------------------

protected void GetConnection(string cnn_db = "NEWBULKSMS")
        {
            try
            {

                string Cnn_Str = "";

                string ServerName = "SHREE-PC";
                string DBUserName = string.Empty;
                string DBPassword = string.Empty;
                DBPassword += "c#" + Convert.ToChar(49);

                string Database = cnn_db;



                Cnn_Str = "Data Source=" + ServerName + "; UID=" + DBUserName + "; PWD=" + DBPassword + "; Database=" + Database+";Integrated Security = True";
                //Cnn_Str = "Data Source=SHREE-PC;Initial Catalog=Project_DB_MNG;Integrated Security=True";
                mstr_ConnectionString = Cnn_Str;

                mobj_SqlConnection = new SqlConnection(mstr_ConnectionString);

                mobj_SqlCommand = new SqlCommand();
                mobj_SqlCommand.CommandTimeout = mint_CommandTimeout;
                mobj_SqlCommand.CommandType = CommandType.StoredProcedure;
                mobj_SqlCommand.Connection = mobj_SqlConnection;
                mobj_SqlConnection.Open();
            }
            catch (Exception ex)
            {
                throw new Exception("Error initializing data class." + Environment.NewLine + ex.Message);
            }
        }

------------------------更新 4---------------- ----------------------

由于您使用带有一个 # 符号的临时 table,这意味着临时 table 将仅在您的会话期间存在。但是,如果您使用 ## 创建一个临时 table,那么每个用户都可以使用那个,并且 none 其他人可以创建一个具有相同名称的临时 table , 但当所有者会话过期或断开连接时,它将被删除。

所以尝试在你的代码中使用:

...
str += " INTO ##Results3 ";
...

并且:

string str1 = " SELECT * FROM ##Results3";
str1 += " SELECT";
str1 += " CampaignName,MobileNo,Message,CharCount,Sender,UserId,DeliveryStatus,";
str1 += "Original_DRStatus,MessageType,ReceiveTime,SendTime,DoneTime,Root,ProviderName
    ,AccountType";
str1 += " FROM ##Results3";
str1 += " WHERE RowNumber BETWEEN('1' - 1) * '500' + 1 AND((('1' - 1) * '500' 
     + 1) + '500') - 1";
str1 += " DROP TABLE ##Results3";