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 选择第一、第二种
        }

你可能感兴趣的:(Ado.Net,数据库,.net)