c# - 使用 SQL Reader 和 Response.Write 执行多个 SQL 命令 INSERT 和 SELECT

标签 c# python asp.net sql-server sqldatareader

Response.Write("文本");在我的页面加载时呈现,但当我单击我的按钮时,它不打印。

我正在尝试执行两个 INSERT 命令和一个 SELECT。我正在使用 Response.Write 来测试 SELECT 命令是否获得了我需要的 ID。

INSERT 命令运行成功(因为数据已发送到数据库)但出于某种原因我无法从 Select 命令访问 SprintBacklogID。我需要在 if 语句中使用 ID 的值,但目前,我只需要能够访问 SELECT 语句中的变量。

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("test");
}

protected void addSprintTasks(object sender, EventArgs e)
{
    Response.Write("another test");
    string taskDescription = Desc.Text;
    string taskHours = Hours.Text;
    string hoursLeft = Hours.Text;
    string ProductStoryID = Request.QueryString["ProductStoryID"];
    string SprintBacklogID = Request.QueryString["SprintBacklogID"];

    int TaskId;

    string connectionString = WebConfigurationManager.ConnectionStrings["GiraConnection"].ConnectionString;
    SqlConnection myConnection = new SqlConnection(connectionString);

    myConnection.Open();

    //string query = "INSERT INTO [SprintTasks] (TaskDescription , EstimatedHours, ProductStoryID, RemainingHours, SprintBacklogID) VALUES (@TaskDescription , @EstimatedHours, @ProductStoryID, @RemainingHours, @SprintBacklogID)";
    //string query1 = "INSERT INTO DailyBurndown (TaskID,Date,HoursRemaining) VALUES (@TaskID, @EstimatedHours, @RemainingHours)";

    //create two insert statements under the one connection so the variables can be referenced

    string sql1 = "BEGIN INSERT INTO [SprintTasks] (TaskDescription , EstimatedHours, ProductStoryID, RemainingHours, SprintBacklogID) VALUES (@TaskDescription , @EstimatedHours, @ProductStoryID, @RemainingHours, @SprintBacklogID)SET @ID = SCOPE_IDENTITY();",
           //sqlTest = @"SELECT SprintID FROM SprintTasks WHERE (SprintTasks.SprintBacklogID = @SprintBacklogID);",
           sql2 = "INSERT INTO [DailyBurndown] (TaskID,Date,HoursRemaining) VALUES (@ID, @EstimatedHours, @EstimatedHours); END;";

    string sql = string.Format("{0}{1}", sql1, sql2);


    string query1 = "SELECT SprintBacklogID FROM SprintTasks WHERE (SprintTasks.SprintBacklogID = @SprintBacklogID)";

    //using sql1 and sql2

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        using (SqlCommand myCommand = new SqlCommand(sql, connection))
        {

            connection.Open();

            myCommand.Parameters.AddWithValue("@TaskDescription", taskDescription);
            myCommand.Parameters.AddWithValue("@EstimatedHours", taskHours);
            myCommand.Parameters.AddWithValue("@RemainingHours", hoursLeft);
            myCommand.Parameters.AddWithValue("@ProductStoryID", ProductStoryID);
            myCommand.Parameters.AddWithValue("@SprintBacklogID", SprintBacklogID);

            //Takes the ID for the task just added to the Sprint Sub Tasks table using Scope Identity
            myCommand.Parameters.Add("@ID", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
            myCommand.ExecuteNonQuery();

            //change over sql statement
            myCommand.CommandText = query1;
            myCommand.ExecuteNonQuery();

            using (SqlDataReader reader = myCommand.ExecuteReader())
            {
                while (reader.Read())
                {
                   Response.Write(reader["SprintBacklogID"].ToString());
                }
            }
            Response.Redirect(Request.RawUrl);
            connection.Close();
        }
    }

    myConnection.Open();

}

最佳答案

您正在使用 Response.Redirect(Request.RawUrl);,因此假设正在缓冲输出:您写入页面的任何内容都将被丢弃,重定向优先。

关于c# - 使用 SQL Reader 和 Response.Write 执行多个 SQL 命令 INSERT 和 SELECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47555711/

相关文章:

c# - 在 Windows 中对 'live' 文件使用 grep

python - 如何在 Linux 上快速将大数据从 C++ 发送到 Python?

python - 在 Scipy(或其他库)中是否有等效于 Python 的 matlab 'idealfilter'?

c# - 无法使用集合初始值设定项初始化类型 '',因为它未实现 'System.Collections.IEnumerable'

javascript - 使用母版页时的卸载事件

c# - 应用程序因 stackoverflow 而崩溃

c# - 将文件拖放到控制台应用程序

c# - .NET Core Web 应用程序,将对象传递给 Controller ​​并进行转换总是失败

c# - 无需 wddm 1.1 即可运行 Windows 7 模拟器

python - 有没有一种优雅的方法可以将组值重新映射到 pandas DataFrame 中的增量系列中?