gpt4 book ai didi

c# - ASP.NET C# 更新查询不工作

转载 作者:太空宇宙 更新时间:2023-11-03 21:36:52 26 4
gpt4 key购买 nike

我正在为大学做一个项目,我想说我是 asp.net 的新手。

请在下面找到我遇到问题的代码。问题是更新功能不起作用。在页面加载中,我有一些选择查询,并将数据库中的数据加载到一些文本区域和文本字段中。这工作正常 - 它加载了我在数据库中手动添加的示例数据。

我有一些按钮可以在点击时更新数据库。

这是按钮的代码:

  <a id="A1"  class="button" onserverclick="box1_Click" runat="server">
<span>Запази полето <img src="notification-tick.gif" width="12" height="12" /></span>
</a>

这是背后的代码:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class admin_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Loading the data from the database

string DatabaseConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["proekt"].ConnectionString;
string sql1_title = "SELECT title FROM home WHERE id=1";
string sql1_image = "SELECT image FROM home WHERE id=1";
string sql1_text = "SELECT text FROM home WHERE id=1";

string sql2_title = "SELECT title FROM home WHERE id=2";
string sql2_image = "SELECT image FROM home WHERE id=2";
string sql2_text = "SELECT text FROM home WHERE id=2";

string sql3_title = "SELECT title FROM home WHERE id=3";
string sql3_image = "SELECT image FROM home WHERE id=3";
string sql3_text = "SELECT text FROM home WHERE id=3";
using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
{
//box1 data load
SqlCommand cmd1_title = new SqlCommand(sql1_title, conn);
conn.Open();
box1_title.Text = (string)cmd1_title.ExecuteScalar();
conn.Close();
SqlCommand cmd1_image = new SqlCommand(sql1_image, conn);
conn.Open();
box1_img.Text = (string)cmd1_image.ExecuteScalar();
conn.Close();
SqlCommand cmd1_text = new SqlCommand(sql1_text, conn);
conn.Open();
box1_text.InnerText = (string)cmd1_text.ExecuteScalar();
conn.Close();

//box2 data load
SqlCommand cmd2_title = new SqlCommand(sql2_title, conn);
conn.Open();
box2_title.Text = (string)cmd2_title.ExecuteScalar();
conn.Close();
SqlCommand cmd2_image = new SqlCommand(sql2_image, conn);
conn.Open();
box2_img.Text = (string)cmd2_image.ExecuteScalar();
conn.Close();
SqlCommand cmd2_text = new SqlCommand(sql2_text, conn);
conn.Open();
box2_text.InnerText = (string)cmd2_text.ExecuteScalar();
conn.Close();

//box3 data load
SqlCommand cmd3_title = new SqlCommand(sql3_title, conn);
conn.Open();
box3_title.Text = (string)cmd3_title.ExecuteScalar();
conn.Close();
SqlCommand cmd3_image = new SqlCommand(sql3_image, conn);
conn.Open();
box3_img.Text = (string)cmd3_image.ExecuteScalar();
conn.Close();
SqlCommand cmd3_text = new SqlCommand(sql3_text, conn);
conn.Open();
box3_text.InnerText = (string)cmd3_text.ExecuteScalar();
conn.Close();
}
}

protected void box1_Click(object sender, EventArgs e)
{
string DatabaseConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["proekt"].ConnectionString;
string sql1 = "UPDATE home SET title=@title, image=@image, text=@text WHERE Id=1";
using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
{
SqlCommand cmd2 = new SqlCommand(sql1, conn);

cmd2.Parameters.AddWithValue("@title", box1_title.Text);
cmd2.Parameters.AddWithValue("@image", box1_img.Text);
cmd2.Parameters.AddWithValue("@text", box1_text.InnerText);

conn.Open();
cmd2.ExecuteNonQuery();
conn.Close();
}
}

protected void box2_Click(object sender, EventArgs e)
{

}


protected void box3_Click(object sender, EventArgs e)
{

}
}

当我更改 box1 的标题然后单击按钮更新数据库时,它实际上是在刷新页面并再次加载示例数据,但我的更改没有保存。

你能帮我解决这个问题吗?没有任何错误。

非常感谢你们。

PS:我注意到当我在浏览器中加载页面然后删除数据加载的整个代码块时,在浏览器中的一个字段中进行更改然后按下按钮它实际上更新了数据库。这很奇怪......

最佳答案

这是因为您需要注意 ASP.NET lifecycle .

你需要检查它是否是 PostBack (当您单击按钮时发生回发)或不...否则您将始终覆盖您的数据。

 protected void Page_Load(object sender, EventArgs e)
{
if (!PostBack)
{
string DatabaseConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["proekt"].ConnectionString;
string sql = "SELECT id,title,image,text FROM home WHERE id in (1,2,3) order by id";
using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
{
conn.Open();

搜索datareader -> 并在这里使用它...将返回的数据保存到datareader 并按 ID 过滤,如果它的项目/标题等像 herehere

          conn.Close();
}

}
}

关于c# - ASP.NET C# 更新查询不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21258585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com