---
title: "使用sqldataadapter 修改数据库数据"
author: "wjxa"
date: "2010-03-11T00:44:07Z"
tags: []
categories: ["编程语言"]
slug: "sqldataadapter"
draft: false
lastmod: "2010-03-11T00:55:57Z"
id: 612465
url: "https://wjxa.iteye.com/blog/612465"
---

```c#

 /// <summary>
    /// 使用SqlDataAdapter适配器修改数据库的数据操作
    /// </summary>
    /// <param name="sender">这是基于Datable填充数据</param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = TextBox1.Text;
        string id = TextBox2.Text;
        //首先是把要修改这张表的数据取出来
        SqlDataAdapter sdat = new SqlDataAdapter("select id,name from xx", sh.GetConnction());
        DataTable dt = new DataTable();
        //用适配器对象fill方法填充到DataTable对象当
         sdat.Fill(dt);
        //然后再用适配器对象updatecommand属性更新数据源当中的记录
        sdat.UpdateCommand = new SqlCommand("update xx set name ='" + name + "' where id='" + id + "'",sh.GetConnction());
        DataRow row = dt.Rows[0];
        row["name"] = name;
        if(sdat.Update(dt)>0)
        Response.Write("<script>alert('修改成功')</script>");
        else
        Response.Write("<script>alert('修改失败')</script>");
    }

```


```c#

 /// <summary>
    /// 使用SqlDataAdapter适配器修改数据库的数据操作
    /// </summary>
    /// <param name="sender">基于DataSet</param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = TextBox1.Text;
        string id = TextBox2.Text;
        //首先是把要修改这张表的数据取出来
        SqlDataAdapter sdat = new SqlDataAdapter("select id,name from category", sh.GetConnction());
        DataSet dt = new DataSet();
        //用适配器对象fill方法填充到DataSet对象当
         sdat.Fill(dt,"xx");
        //然后再用适配器对象updatecommand属性更新数据源当中的记录
        sdat.UpdateCommand = new SqlCommand("update xx set name ='" + name + "' where id='" + id + "'",sh.GetConnction());
        dt.Tables["xx"].Rows[0]["name"] = "id";
       if(sdat.Update(dt.Tables[0])>0)
        Response.Write("<script>alert('修改成功')</script>");
        else
        Response.Write("<script>alert('修改失败')</script>");
    }

```


           <span style="color:red">我自己总结的两种方法！！ 太晚了洗洗睡吧 ^_^</span>