【问题标题】:Executing multiple SQL commands INSERT and SELECT using SQL Reader and Response.Write使用 SQL Reader 和 Response.Write 执行多个 SQL 命令 INSERT 和 SELECT
【发布时间】:2017-11-29 14:57:38
【问题描述】:

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();

}

【问题讨论】:

  • 这是因为Button Clicks触发了所谓的PostBacks
  • 考虑使用逐字字符串文字来创建多行字符串,而不必连接字符串:docs.microsoft.com/en-us/dotnet/csharp/language-reference/…
  • 你为什么使用Response.Write?您无法控制最终在标记中的位置。为什么不将 HTML 附加到页面上的现有控件?为什么您在某些地方正确地遵循 IDisposable 模式(将您的 IDisposable 对象包装在 using 语句中)而不是其他地方?
  • 当您尝试执行 Select 查询时,将其更改为 ExecuteScalar() ExecuteNonQuery - is used for Inserts Updates and Deletes
  • 当您说“由于某种原因我无法从 Select 命令中访问 SprintBacklogID”时,您的意思是您从 SELECT 得到零记录吗?有错误吗?是什么行为?

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


【解决方案1】:

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

【讨论】:

  • 在取出这个重定向时,它抱怨连接没有关闭,即使它关闭了?我已经添加了两个连接打开/关闭执行的任一侧,但仍然没有运气
  • @KellyM1996 您不需要手动关闭连接。 using 语句会处理这个问题。不过,请确保您实际上使用了 using 语句,因为您是为其中一个连接而不是另一个连接使用的。不过,您仍然需要手动打开它。
猜你喜欢
  • 1970-01-01
  • 2012-07-17
  • 2011-01-22
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
  • 2020-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多