我正在爲大學一個項目,我想說的是,我在asp.net
很新。ASP.NET C#更新查詢不工作
請找到下面的代碼,我遇到的問題。問題是更新功能不起作用。在頁面加載中,我有一些選擇查詢,並將數據從數據庫加載到一些textareas和textfields中。這工作正常 - 它會加載手動添加到我的數據庫中的示例數據。
我有按鈕應該更新點擊數據庫。
這是按鈕的代碼:
<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 [email protected], [email protected], [email protected] 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:我注意到當我在瀏覽器中加載頁面,然後刪除數據加載的整個代碼塊時,在其中一個字段中對瀏覽器進行更改,然後按實際更新的按鈕數據庫。這是很奇怪......
避免** 9 **跳到數據庫; 'SELECT id,title,image,text FROM home WHERE id in(1,2,3)order by id' and filter in the client –
在一個建議中,打開連接一次,一次完成所有查詢,然後關閉它。 – StuartLC