ADO.NET 数据更新

 

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();

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值