using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace ADO.updata
{
class Program
{
static void Main(string[] args)
{
/// <summary>
/// 更新数据
/// </summary>
SqlConnection cnn = new SqlConnection(@"Server=MICHAELYANG\SQLEXPRESS;Integrated Security=True;" + "Database=NorthwindEn");//创建连接
SqlDataAdapter thisAdapter=new SqlDataAdapter ("select CustomerID,CompanyName from Customers",cnn);//创建DATAADAPTER对象
SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);//重构SQL用于更新
thisAdapter.UpdateCommand = thisBuilder.GetUpdateCommand();
DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet ,"Customers");//填充数据(内存)
Console.WriteLine("name before change:{0}", thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]);//读取映射到内存中的数据
thisDataSet.Tables["Customers"].Rows[9]["CompanyName"] = "Test, Inc.";//赋值
thisAdapter.Update(thisDataSet, "Customers");//更新
Console.WriteLine("name after change:{0}", thisDataSet.Tables["Customers"].Rows[9]["CompanyName"]);//读取修改后的数据
cnn.Close();//关闭连接
Console.WriteLine("Prgram finished,press enter/Retrun to Continue...");
Console.ReadKey();
}
}
}