zoukankan      html  css  js  c++  java
  • 用DataSet方式更新数据库表

    /*  用DataSet的方式更新数据库表
     *  注意:用DataSet更新数据库表的时候,该表必须指定主键或者是唯一列
     */ 
    string connString = "Data Source=(local);Initial Catalog=Linq;Integrated Security=SSPI";//用windows用户登录
    using (SqlConnection conn = new SqlConnection(connString))
    {
        conn.Open();
        using (SqlCommand cmd = conn.CreateCommand())
        {
            cmd.CommandText = "select * from orders";
            DataSet dataset = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            /*把查询出来的结果填充到dataset中,并指定一个表面:orders
             *注意:
             *      如果不指定表名,直接用adapter.Fill(dataset)得方式填充,DataTable接收的时候根据索引号
             *      DataTable table = dataset.Tables[0];
             */
            adapter.Fill(dataset,"orders");
            DataTable table = dataset.Tables["orders"];
            //把第一行数据的城市更新为“攀枝花”
            DataRow row = table.Rows[0];
            row["city"] = "攀枝花";
            /* builder:用户更新数据库的时候自动创建SqlCommand
             * 具体可以查看:
             *      builder.GetUpdateCommand();
             *      builder.GetInsertCommand();
             *      builder.GetDeleteCommand();
             */
            SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
            /* 这里update的时候可以有多种方式:
             *      adapter.Update(dataset,"orders");指定dataset的表名更新
             *      adapter.Update(table");更新table
             *      adapter.Update(dataset);直接更新整个dataset
             */
            adapter.Update(dataset,"orders");
            Console.WriteLine("更新成功");
            Console.ReadKey();
        }
    }
  • 相关阅读:
    self 和 super 关键字
    NSString类
    函数和对象方法的区别
    求两个数是否互质及最大公约数
    TJU Problem 1644 Reverse Text
    TJU Problem 2520 Quicksum
    TJU Problem 2101 Bullseye
    TJU Problem 2548 Celebrity jeopardy
    poj 2586 Y2K Accounting Bug
    poj 2109 Power of Cryptography
  • 原文地址:https://www.cnblogs.com/yangyong-yy/p/5416330.html
Copyright © 2011-2022 走看看