SqlDataAdapter介绍与创建

目录

SqlDataAdapter是什么:

如何提供桥接:

4个重要属性:

4个创建方式:

  1、设置SelectCommand

 2、通过一个SqlCommand对象来实例化SqlAdapter

 3、查询语句和连接对象来实例化一个SqlAdapter

 4、查询语句和连接字符串,构建一个adapter(比较耗时)

 完整实例:


SqlDataAdapter是什么:

        适配器(桥接器),DataSet 数据之间用于检索和保存数据的桥梁;

        SqlDataAdapter类    填充DataSet以及更新数据源的一组数据库命令和一个数据库连接

        SqlDataAdapter        是DataSet和SQLServer之间的桥接器

如何提供桥接:

        Fill()填充到  DataSet中,UpDate()    更改提交到数据库,使数据保持一致。

        SqlConnection   SqlCommand两个对象一起使用,以此提高访问速度。

4个重要属性:

        SelectCommand        查询记录,设置或生成一个对象SqlCommand

        InsertCommand        插入记录

        UpdateCommand        更新数据

        DeleteCommand        删除记录

        SqlDataAdapter 对数据的操作也是建立在SqlCommand基础之上的

4个创建方式:

        需要引用命名空间【using System.Data;】、【using System.Data.SqlClient;】

  1、设置SelectCommand

            //连接字符串
            string connStr = ConfigurationManager.ConnectionStrings
                 ["connStr"].ConnectionString;
            //t-sql语句
            string sql = "select * from tesTable";
            //连接数据库
            SqlConnection conn = new SqlConnection(connStr);

            //1、设置SelectCommand
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand(sql, conn);

 2、通过一个SqlCommand对象来实例化SqlAdapter

            //连接字符串
            string connStr = ConfigurationManager.ConnectionStrings
                 ["connStr"].ConnectionString;
            //t-sql语句
            string sql = "select * from tesTable";
            //连接数据库
            SqlConnection conn = new SqlConnection(connStr);

            //2、通过一个SqlCommand对象来实例化SqlAdapter
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter2 = new SqlDataAdapter(cmd);

 3、查询语句和连接对象来实例化一个SqlAdapter

            //连接字符串
            string connStr = ConfigurationManager.ConnectionStrings
                 ["connStr"].ConnectionString;
            //t-sql语句
            string sql = "select * from tesTable";
            //连接数据库
            SqlConnection conn = new SqlConnection(connStr);

            //3、查询语句和连接对象来实例化一个adapter
            SqlDataAdapter adapter3 = new SqlDataAdapter(sql, conn);

 4、查询语句和连接字符串,构建一个adapter(比较耗时)

            //连接字符串
            string connStr = ConfigurationManager.ConnectionStrings
                 ["connStr"].ConnectionString;
            //t-sql语句
            string sql = "select * from tesTable";
            
            //4、查询语句和连接字符串,构建一个adapter
            SqlDataAdapter adapter4 = new SqlDataAdapter(sql, connStr);

 完整实例:

            如果使T-SQL查询语句,选择第三种
            带参数,添加参数,操作SqlCommand 选择第一、第二种

        static void TestDataAdapter()
        {
            //连接字符串
            string connStr = ConfigurationManager.ConnectionStrings
                ["connStr"].ConnectionString;
            //查询语句
            string sql = "select * from tesTable";
            SqlConnection conn = new SqlConnection(connStr);

            //1.设置SelectCommand
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand(sql, conn);
            //2.通过一个SqlCommand对象来实例化SqlAdapter
            SqlCommand cmd = new SqlCommand(sql,conn);
            SqlDataAdapter adapter1 = new SqlDataAdapter(cmd);
            //3.查询语句和连接对象来实例化一个adapter
            SqlDataAdapter adapter2 = new SqlDataAdapter(sql, conn);
            //4.查询语句 和 连接字符串,构建一个adapter(比较耗时)
            SqlDataAdapter adapter3 = new SqlDataAdapter(sql, connStr);

            //如果使T-SQL查询语句,选择第三种
            //带参数,添加参数,操作SqlCommand 选择第一、第二种
        }

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

默九思

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值