Datatable不能更新

本文关键字:更新 不能 Datatable | 更新日期: 2023-09-27 17:49:38

我想更新包含两个文本和一个图像所需的文件名的数据库。

问题是图像和文件名更新,但其他两个文本值标题和正文不会受到影响,不改变以前的值。visual studio也没有任何问题,执行命令的消息显示它执行了命令,但除了图像更改之外什么也没有。

    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["user"] == null)
            Response.Redirect("~/default.aspx");
        if (Request .QueryString ["action"]=="edit")
        {
            Panel1.Visible = true;
        }
        if (Request.QueryString["edit"] != null)
        {

            Panel1.Visible = true;
            SqlConnection con2 = new SqlConnection();
            con2.ConnectionString =GNews.Properties.Settings.Default.connectionstring;
            DataTable dt3 = new DataTable();
            con2.Open();
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand("select * from loadpost_view where Postid=" + Request.QueryString["edit"].ToString () + "", con2);
            myReader = myCommand.ExecuteReader();

            while (myReader.Read())
            {
                title_txt.Text=myReader ["Title"].ToString ();
                bodytxt.Text = myReader["Body"].ToString();
            }
            con2.Close();
        }
protected void btn_addpost_Click(object sender, EventArgs e)
    {
string title= title_txt .Text ;
string body=bodytxt .Text ;
if (Request.QueryString["edit"] != null)
{
    string message;
    string filename = thumb_uploader.FileName;
    string path = HttpContext.Current.Server.MapPath("~") + "''Thumb";
    string exup = System.IO.Path.GetExtension(thumb_uploader.FileName);
    string[] ext = { ".jpg", ".png", ".jpeg" };
    if (Array.IndexOf(ext, exup) < 0)
    {
        message = "not correct.";
    }
    if (thumb_uploader.FileBytes.Length / 1024 > 400)
    {
        message = "not currect.";
    }
    while (System.IO.File.Exists(path + "''" + filename + exup))
    {
        filename += "1";
    }
    savepath = path + "''" + filename;
    if (thumb_uploader.HasFile)
    {
        thumb_uploader.SaveAs(savepath);
        thumb = thumb_uploader.FileName;
        SqlCommand command;
        SqlDataAdapter da;
        SqlConnection con3 = new SqlConnection();
        con3.ConnectionString = GNews.Properties.Settings.Default.connectionstring;
        command = new SqlCommand();
        command.Connection = con3;
        da = new SqlDataAdapter();
        da.SelectCommand = command;
        command.CommandText = "UPDATE tbl_post SET Title=@title ,Body=@body ,Thumb=@thu Where Postid=" + Request.QueryString["edit"].ToString();
        con3.Open();
        command.Parameters.AddWithValue("@title", title );
        command.Parameters.AddWithValue("@body", body );
        command.Parameters.AddWithValue("@thu", thumb_uploader .FileName);
        command.ExecuteNonQuery();
        con3.Close();
        message = "its ok.";
        lbl_result.Text = message;
   }
   else
   {
        using (SqlConnection con3 = new SqlConnection(GNews.Properties.Settings.Default.connectionstring))
        {
            string sql = "update tbl_post SET Title=@title ,Body=@body  Where Postid=@postid" ;
            using (SqlCommand command = new SqlCommand(sql, con3))
            {
                con3.Open();
                command.Parameters.AddWithValue("@title", title);
                command.Parameters.AddWithValue("@body", body);
                command.Parameters.AddWithValue("@postid",  Request.QueryString["edit"].ToString());
                command.ExecuteNonQuery();
                con3.Close();
                message = "its ok.";
                lbl_result.Text = message;
            }
        }
   }
}

Datatable不能更新

我找到了答案,我需要这个代码来包含我的页面加载读取数据库,所以当我点击更新按钮时它不会这样做。我的意思是问题都是关于post back的。

 if (!Page.IsPostBack)
        {
            if (Request.QueryString["edit"] != null)
            {

                Panel1.Visible = true;
                SqlConnection con2 = new SqlConnection();
                con2.ConnectionString = GNews.Properties.Settings.Default.connectionstring;
                DataTable dt3 = new DataTable();
                con2.Open();
                SqlDataReader myReader = null;
                SqlCommand myCommand = new SqlCommand("select * from loadpost_view where Postid=" + Request.QueryString["edit"].ToString() + "", con2);
                myReader = myCommand.ExecuteReader();

                while (myReader.Read())
                {
                    title_txt.Text = myReader["Title"].ToString();
                    bodytxt.Text = myReader["Body"].ToString();
                }
                con2.Close();
            }
        }