当前位置:Gxlcms > 数据库问题 > 数据适配器SqlDataAdapter学习笔记

数据适配器SqlDataAdapter学习笔记

时间:2021-07-01 10:21:17 帮助过:5人阅读

SqlDataAdapter(数据适配器) 是 DataSet 和 SQL Server 之间的桥接器,用于检索和保存数据。SqlDataAdapter 与 SqlConnection 和 SqlCommand 一起使用,以便在连接到 SQL Server 数据库时提高性能。下面结合我们做的医院药库管理系统中厂商资料的新建和维护这一功能来说明适配器的用法。

 private void 厂商资料新建和维护_Load(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();     //声明并实例化SQL连接;
            sqlConnection.ConnectionString = "Data Source=(local);Initial Catalog=yyao;Integrated Security=True"; //在字符串变量中,描述连接字符串所需的服务器地址、数据库名称、集成安全性(即是否使用Windows验证);
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT * FROM tb_Firm;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); //声明并实例化SQL数据适配器;
            sqlDataAdapter.SelectCommand = sqlCommand;//将SQL数据适配器的查询命令属性指向SQL命令;
            DataTable csTable = new DataTable();
            sqlConnection.Open();
            sqlDataAdapter.Fill(csTable);//SQL数据适配器读取数据,并填充厂商资料数据表;
            sqlConnection.Close();
            this.dgv_cszl.DataSource = csTable;
        }

数据适配器SqlDataAdapter学习笔记

标签:local   这一   资料   str   dataset   ext   字符串   使用   rate   

人气教程排行