数据库学习任务四:数据读取器对象SqlDataReader、数据适配器对象SqlDataAdapter、数据集对象DataSet...

       数据库应用程序的开发流程一般主要分为以下几个步骤:

  1. 创建数据库
  2. 使用Connection对象连接数据库
  3. 使用Command对象对数据源执行SQL命令并返回数据
  4. 使用DataReader和DataSet对象读取和处理数据源的数据

 

     在与数据库的交互中,要获得数据库访问的结果可以两种方法实现,一是通过DataReader对象从数据源中获取数据并进行处理;二是通过DataSet对象将数据存储在内存中进行处理。

     1.SqlDataReader对象

SqlDataReader对象可以顺序地从查询结果中读取记录,其特点是单向向前,速度快,占内存少。这里要注意SqlCommand对象不能用new初始化,必须调用SqlCommand对象的ExecuteReader()方法初始化。

 

 

 

      其中 Item 属性有两种使用方法:

 

 

 

 

 

【例】使用SqlDataReader对象读取数据,判断登录是否成功

 protected void btnLogin_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ASUS\Desktop\案例\EBookShop\App_Data\ShopBookDB.mdf;
Integrated Security=True;User Instance=True
"; SqlCommand com = new SqlCommand(); com.Connection = con; com.CommandText = "select * from users where user_name ='" + txtName.Text.Trim() + "' and password='" + txtPwd.Text.Trim() + "'"; con.Open(); SqlDataReader sdr = com.ExecuteReader(); try { if (sdr.HasRows) { Response.Write("<script>alert('登录成功!')</script>"); } else { Response.Write("<script>alert('登录失败')</script>"); } sdr.Close(); con.Close(); } catch (Exception) { Response.Write("<script>alert('数据库无法连接')</script>"); con.Close(); } }

 

 

     2.SqlDataAdapter对象

SqlDataAdapter对象是一种用来充当数据集与实际数据源之间的桥梁的对象,使用SqlDataAdapter对象在应用程序和数据库之间通讯,数据适配器可以将数据从数据库读入数据集,也可以见数据集中已更改的数据写回数据库。

 

 

 

 

 

     3.DataSet对象

DataSet对象是数据库数据的内存驻留表示形式,无论数据源是什么,都会提供一致的关系编程模型。它可以用户多种不同的数据源:用于XML数据,或用于管理应用程序本地的数据。一个DataSet对象表示包括相关表、约束和表间关系在内的整个数据集。

 【例】使用SqlDataAdapter对象读取数据,利用DataSet对象显示数据

protected void btnDS_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ASUS\Desktop\案例\EBookShop\App_Data\ShopBookDB.mdf;Integrated Security=True;User Instance=True";
        SqlCommand com = new SqlCommand();
        com.Connection = con;
        com.CommandText = "select * from users";
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = com;
        DataSet ds = new DataSet();
        con.Open();
        try
        {
            ds.Clear();
            da.Fill(ds,"users");
            if (ds.Tables["users"].Rows.Count == 0)
            {
                Response.Write("没有查询到数据,请重试!");

            }
            else {
                Response.Write("用户名" + "密码"+"<br/>");
                for (int i = 0; i < ds.Tables["users"].Rows.Count;i++ ) {
                    Response.Write(ds.Tables["users"].Rows[i]["user_name"]);
                    Response.Write(ds.Tables["users"].Rows[i]["password"]);
                    Response.Write("<br/>");
                }

            }
            con.Close();
        }
        catch (Exception)
        {
            Response.Write("<script>alert('数据库无法连接')</script>");
            con.Close();
        }
    }

 

转载于:https://www.cnblogs.com/yankyblogs/p/6871523.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值