如何更新数据库中的文本框值 [英] how can update textbox values in database

查看:96
本文介绍了如何更新数据库中的文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面加载中检索文本框中的数据库值

i retrieving database values in textbox in page load

SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("select * from Employee", cn);
DataSet ds = new DataSet();
da.Fill(ds);
TextBox1.Text = ds.Tables[0].Rows[0][1].ToString();
TextBox2.Text = ds.Tables[0].Rows[0][2].ToString();
TextBox3.Text = ds.Tables[0].Rows[1][1].ToString();
TextBox4.Text = ds.Tables[0].Rows[1][2].ToString();
TextBox5.Text = ds.Tables[0].Rows[2][1].ToString();





它将在文本框中显示数据库值。现在我想编辑文本框文本,点击更新按钮后databsae表应该更新。

请帮帮我



it will show the database values in textbox. now i want to edit textbox text and after click the update button databsae table should be update .
please help me

推荐答案

请使用数据绑定更好的方法,或创建一个新的SQL命令进行更新,并将所有文本框值传递给命令并调用执行更新,如下所示:



Please use Data binding for a better approach, or create a new SQL command for update, and pass all your textboxes values to the command and call execute update like this:

public bool ExecuteCommand ()
{
   using (sqlConnection)
   {
      try
      {
         sqlConnection.Open();
         using (var sqlCommand = new SqlCommand(CommandText, sqlConnection))
         {
             sqlCommand.CommandText = 'your update cmd';
             
             sqlCommand.ExecuteNonQuery()
         }
      }
      catch (Exception)
      {
         //Handle Exception Here
      }

   }
}


仔细关注此帖子的问题和答案:

如何将数据库中的数据显示到文本框中并进行更新 [ ^ ]
Follow question ans answer of this thread carefully:
How to display data from database into textbox, and update it[^]


为您的文本框值写入更新功能,例如

Write Update function for your textbox value like
public int update_Record()
       {
           DB_Connection();
           int i;
           string QRY = "UPDATE personalInfo_Master SET village='" + Village + "', taluka='" + Taluka + "',dist='" + District + "',state='" + State + "'WHERE name='" + Name + "'";
           SqlCommand CMD = new SqlCommand(QRY, con);
           i = CMD.ExecuteNonQuery();
           return i;
       }


这篇关于如何更新数据库中的文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆