【发布时间】: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