蜗牛—C#程设之DataAdapter对象

使用DataAdapter和DataSet来读取数据表JBQK中的数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace shiyan11
{
    class Program
    {
        static void Main(string[] args)
        {
            string strCon = @"Data Source = .\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True;";
            SqlConnection sqlCon = new SqlConnection(strCon);
            try
            {
                sqlCon.Open();
                string sqlStr = @"select No,Name,Grade from JBQK";
                SqlCommand sqlCmd = new SqlCommand(sqlStr, sqlCon);
                SqlDataAdapter sda = new SqlDataAdapter(sqlCmd);  
                DataSet ds = new DataSet();
                sda.Fill(ds);    //填充数据集,实质是填充ds中的第0个表
                string sltResult = "";
                DataTable dt = ds.Tables[0];
                Console.WriteLine("基本情况数据表查询结果如下:");
                for (int i = 0; i < dt.Rows.Count;i++ ) {
                    //逐行读取,每行通过字段名或者索引来访问   
                    sltResult += "第" + (i + 1) + "记录:" + dt.Rows[i][0].ToString() + "\t" 
                        + dt.Rows[i]["Name"].ToString() + dt.Rows[i][2].ToString() + "\n";
                }
                Console.WriteLine(sltResult);
            }
            catch (Exception e) 
            {
                Console.WriteLine("失败!!");
            }
            sqlCon.Close();
            Console.Read();
            
            
        }
    }
}

删除JBQK表中的第一条数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace shiyan11
{
    class Program
    {
        static void Main(string[] args)
        {
            string strCon = @"Data Source = .\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True;";
            SqlConnection sqlCon = new SqlConnection(strCon);
            try
            {
                sqlCon.Open();
                string sqlStr = @"select No,Name,Grade from JBQK";
                SqlCommand sqlCmd = new SqlCommand(sqlStr, sqlCon);
                SqlDataAdapter sda = new SqlDataAdapter(sqlCmd);  
                DataSet ds = new DataSet();
                sda.Fill(ds);    //填充数据集,实质是填充ds中的第0个表
                string sltResult = "";
                
                //----------------------------------------------
                //以sda为参数来初始化SqlCommandBuilder实力
                SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                //删除DataSet中数据表JBQK中的第一行数据
                ds.Tables[0].Rows[0].Delete();
                //调用Update方法,以DataSet中的数据更新数据库
                sda.Update(ds, ds.Tables[0].ToString());
                ds.Tables[0].AcceptChanges();

                //----------------------------------------------
                DataTable dt = ds.Tables[0];
                Console.WriteLine("基本情况数据表查询结果如下:");
                for (int i = 0; i < dt.Rows.Count;i++ ) {
                    //逐行读取,每行通过字段名或者索引来访问   
                    sltResult += "第" + (i + 1) + "记录:" + dt.Rows[i][0].ToString() + "\t" 
                        + dt.Rows[i]["Name"].ToString() + dt.Rows[i][2].ToString() + "\n";
                }

                Console.WriteLine(sltResult);
            }
            catch (Exception e) 
            {
                Console.WriteLine(e.ToString());
            }
            sqlCon.Close();
            Console.Read();
            
            
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值