简体   繁体   English

如何打印在存储过程中调用的参数和查询?

[英]How do I print the parameters and query that was called in the stored procedure?

I have a set aspx.cs codes to call a stored procedure to populate a grid view in my front end website. 我有一组aspx.cs代码来调用存储过程,以在前端网站中填充网格视图。 However during the run gridview is not populated but there are no errors. 但是,在运行期间,不会填充gridview,但是没有错误。 So I would like to know how to print the SQL query that was executed with the parameters as well. 因此,我想知道如何打印使用参数执行的SQL查询。 Thanks 谢谢

The reason is being when I run with the existing data it populates however if new data are added it causes the gridview to be not populated at all. 原因是当我使用现有数据填充时,但是如果添加了新数据,则将导致根本不填充gridview。

using (SqlConnection conn = new SqlConnection(dbConn))
            {
                using (SqlCommand cmd = new SqlCommand(spretrieve, conn))
                {
cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@param1", SqlDbType.VarChar).Value = selectedDATE;
                        cmd.Parameters.Add("@param2", SqlDbType.VarChar).Value = selectedLVL2RISK;
                        cmd.Parameters.Add("@param3", SqlDbType.VarChar).Value = selectedORSA;
                        cmd.Parameters.Add("@param4", SqlDbType.VarChar).Value = selectedDPT;
                        string query = cmd.CommandText;

                        //Populate ORSA_ASSESSMENTS grid view
                        conn.Open();
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        DataSet ds = new DataSet();
                        da.Fill(ds);
                        GRID.DataSource = ds.Tables[0];
                        GRID.DataBind();
              }
}
using (SqlConnection conn = new SqlConnection(dbConn))
        {
            using (SqlCommand cmd = new SqlCommand(spretrieve, conn))
            {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@param1", SqlDbType.VarChar).Value = selectedDATE;
                    cmd.Parameters.Add("@param2", SqlDbType.VarChar).Value = selectedLVL2RISK;
                    cmd.Parameters.Add("@param3", SqlDbType.VarChar).Value = selectedORSA;
                    cmd.Parameters.Add("@param4", SqlDbType.VarChar).Value = selectedDPT;
                    string query = cmd.CommandText;

                    foreach (SqlParameter p in cmd.Parameters)
                    {
                         query = query.Replace(p.ParameterName, p.Value.ToString());
                    }

                    //query variable store sql statement with parameter value.       
                    //Populate ORSA_ASSESSMENTS grid view
                    conn.Open();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    GRID.DataSource = ds.Tables[0];
                    GRID.DataBind();
          }
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM