C#使用SqlDataAdapter的Update方法更新数据

来源:互联网 发布:mysql数据库5.5下载 编辑:程序博客网 时间:2024/06/05 07:40

sql语法:

Update one Column in a Row
更新记录行中的一个栏目

We want to add a first name to the person with a last name of "Rasmussen":
我们将给person表中LastName为"Rasmussen"的数据添加一个first name值

UPDATE Person SET FirstName = 'Nina'
WHERE LastName = 'Rasmussen'

Update several Columns in a Row
给一记录行更新多个栏目

We want to change the address and add the name of the city:
我们将个人信息中Lastname为"Rasmussen"的所在城市名和地址做一次改变:

UPDATE Person
SET Address = 'Stien 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen'

Update 表名 set (*) value(把表中的所有字段的值对应填写);


例1:

using System.Data.SqlClient;

namespace UpdatingData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
btnUpdate.Click += new EventHandler(btnUpdate_Click);
UpdateData();
}


string sConnection = "Data Source=scott;Initial Catalog=northwind;Persist Security Info=True;User ID=sa;Password=sa123";
DataSet dsSet = new DataSet();
SqlDataAdapter sdaAdapter = null;
SqlCommandBuilder scbBuilder = null;
private void UpdateData()
{
//建立Connection
SqlConnection scConnection = new SqlConnection(sConnection);
//建立Command
SqlCommand scCommand = scConnection.CreateCommand();
scCommand.CommandText = "select customerID,contactName from customers";
//建立Adapter
sdaAdapter = new SqlDataAdapter(scCommand);

//该对象负责生成用于更新数据库的SQL语句,不必自己创建这些语句
scbBuilder = new SqlCommandBuilder(sdaAdapter);

//得到数据
sdaAdapter.Fill(dsSet, "customers");
dgvView.DataSource = dsSet.Tables["customers"];

}

void btnUpdate_Click(object sender, EventArgs e)
{
//设置值
dsSet.Tables["customers"].Rows[3]["contactName"] = "Thomas Hardy";
//更新数据(UpDate方法自动遍历DataTable中的行,以找出需要对数据库作出变动)
//Rows集合中每个DataRow对象都具有属性RowState,可以跟踪此行是否已删除、添加、修改,还是未作变动。所作的任何变化都会反映到数据库中。
sdaAdapter.Update(dsSet, "customers");
dgvView.DataSource = dsSet.Tables["customers"];

}
}

2


其实非常简单,就是用SqlDataAdapter的update方法就行了。主要代码例如下所示:

private void update()

{

string link = ConfigurationSettings.AppSettings["link_local"].ToString();

SqlConnection conn = new SqlConnection(link);

SqlDataAdapter da = new SqlDataAdapter("SELECT order_id, contract FROM linhai", conn);

DataSet ds = new DataSet();

da.Fill(ds, "linhai");

da.UpdateCommand = new SqlCommand("UPDATE linhai SET contract = @contract " + "WHERE order_id = @order_id" , conn);

da.UpdateCommand.Parameters.Add("@contract", SqlDbType.NVarChar, 15, "contract");

da.UpdateCommand.Parameters.Add("@order_id", SqlDbType.NVarChar, 15, "order_id");

ds.Tables["linhai"].Rows[0]["contract"] = "PPP";

da.Update(ds.Tables[0]);

}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 儿童不能值机怎么办 飞机上旅客刁难怎么办 飞机上婴儿哭闹怎么办 飞机上婴儿啼哭怎么办 飞机儿童陪护手续怎么办 孕妇感冒鼻子堵怎么办 孕妇用了眼药水怎么办 孕妇用眼药水了怎么办 错过了登机时间怎么办 过了登机时间怎么办 孩子不喜欢做作业怎么办 宝宝腿不一样粗怎么办 一个小腿肿了怎么办 左右小腿不一样粗怎么办 小孩不敢自己睡怎么办 小孩不一个人睡怎么办 晚上一个人睡觉害怕怎么办 孩子自己睡害怕怎么办 小孩晚上睡觉怕怎么办 小孩分房睡害怕怎么办 坐飞机不认识字怎么办 科目一不识字怎么办 孩子不自己睡觉怎么办 70岁老人坐高铁怎么办 八岁儿童怎么办护照 第一次坐飞机有点紧张怎么办 坐飞机恐高耳鸣怎么办 火车坐过站怎么办 凌晨的火车应该怎么办 高铁坐过一站怎么办 宝宝断奶瘦了怎么办 第一次坐飞机怕晕机怎么办 焦虑症怕坐飞机怎么办 新生宝宝老是哭闹怎么办 宝宝坐飞机后哭闹怎么办 飞机经济舱不支持退票怎么办 证件被单位扣押怎么办 小孩高烧41度怎么办 国际航班未成年托管怎么办手续 如果飞机上发烧怎么办 不到二岁飞机票怎么办