如何一一并发运行sql查询

时间:2019-03-11 09:26:20

标签: sql sql-server tsql c#-4.0 asp.net-4.5

我正在开发类似于“每日SMS日志”的页面,用于检索有关在后台运行的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

在这里,当我取消注释第一行到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_Querry(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 = GetDataTableByQuerry(str);
            return dt;
        }

        public void Execute_Querry(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 GetDataTableByQuerry(string str_querry)
        {
            try
            {
                mobj_SqlCommand.CommandText = str_querry;
                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 querry_str)
        {
            try
            {
                mobj_SqlCommand.CommandType = CommandType.Text;
                mobj_SqlCommand.CommandText = querry_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 ------------------- -------------------

SQL Management Studio Query Result

1 个答案:

答案 0 :(得分:1)

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

因此请尝试在您的代码中使用

...
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";