非链接方式访问数据库--查询的数据集用Dataset来存储。
                        
                            时间:2021-07-01 10:21:17
                            帮助过:2人阅读
							                        
                     
                    
                    
                     void Button_Click_1(
object sender, RoutedEventArgs e)
        {
            //非链接方式访问数据库,
            //1创建连接对象(连接字符串)
            using (SqlConnection conn = 
new SqlConnection(SQLHelper.ConnectionString))
            {
                //2.创建数据适配器对象
                using (SqlDataAdapter sda = 
new SqlDataAdapter(
"select * from Student",conn))
                { 
                    //3.打开数据库连接(这一步其实可以省略)
                    conn.Open();
                    DataSet ds=
new DataSet();
                    //4.发送命令
                    sda.Fill(ds);
                    //5.关闭连接
                    //备注:因为这里使用了using代码块,连接的关闭步骤可以省去
                    DataRowCollection drCollection = ds.Tables[
0].Rows;
                    for (
int i = 
0; i < drCollection.Count; i++
)
                    {
                      DataRow dr=
drCollection[i];
                      MessageBox.Show(dr["s_Name"].ToString());
                    }
                
                }
            
            }
           
        }
 注意:dataset是占内存的,我们查询你的数据有多少,就占多少内存。
非链接方式访问数据库--查询的数据集用Dataset来存储。
标签: