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/