数据库文件提取 dataset 和 SqlDataReader 的使用

数据库文件 dataset 离线数据代码,用来把服务器拿到的数据加载在本地服务器当中,

 using ( SqlConnection conn =
                 new SqlConnection ("Data Source=.; Initial Catalog=Title;User ID=sa;Password=
                 new SqlConnection ("Data Source=.; Initial Catalog=Title;User ID=sa;Password=
                 new SqlConnection ("Data Source=.; Initial Catalog=Title;User ID=sa;Password=mima"))
                 {
                     conn.Open();
                     using (SqlCommand cmd = conn.CreateCommand())
                     {
                         //写数据库的命令
                         cmd.CommandText = "select *from T_Student where Age > @age";
                         cmd.Parameters.Add(new SqlParameter ("@age" , 60));
                         //创建数据库接合器 把cmd查询到的结构给adpater 
                         SqlDataAdapter adapter = new SqlDataAdapter(cmd);


                         //创建本地的一个集合进行接收
                         //dataSet类表示数据在内存中缓存
                         DataSet list = new DataSet();
                         
                         // 把command的结果 填到 到list中
                         adapter.Fill(list);


                         //下面是数据的遍历 
                         //DeataTable 表示内存当中的数据表
                         DataTable table = list.Tables[0];
                         // 获取当中行的  集合
                         DataRowCollection rows = table.Rows;
                         //遍历当中的行
                         for (int i = 0; i < rows.Count; i++)
                         { 
                             //获取当rows当中一行的数据
                             DataRow row = rows[i];
                             int age = (int ) row["Age"];
                             string name = (string)row["Name"];


                             MessageBox.Show(age + " " + name);
                         }  
                                     
                     }
                 }
      

// 数据库列查询代码

  using (SqlCommand cmd = conn.CreateCommand())
                { 

                    //给查询询命令提供参数
                    cmd.CommandText = "select age from T_Student where Name =@name";
                    cmd.Parameters.Add(new SqlParameter( "@name",txtShow.Text));   
                 
                    using ( SqlDataReader read = cmd.ExecuteReader())
                    { 
                        //数据库查询
                        while (read.Read())
                        { 
                            int age = read.GetInt32(0);
                            MessageBox.Show(age.ToString ());
                        }
                    }
                }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值