zoukankan      html  css  js  c++  java
  • 数据库学习任务四:数据读取器对象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:UsersASUSDesktop案例EBookShopApp_DataShopBookDB.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:UsersASUSDesktop案例EBookShopApp_DataShopBookDB.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();
            }
        }
  • 相关阅读:
    CSS3——复杂选择器
    单元测试覆盖率设置
    你必须了解的「架构」小历史
    js正则表达式:学习网址和部分正则验证
    转: js实现全角半角检测的方法
    Linux and the Unix Philosophy(1)
    HTML DOM 对象
    理解css中的 content:" " 是什么意思
    JS
    js
  • 原文地址:https://www.cnblogs.com/yankyblogs/p/6871523.html
Copyright © 2011-2022 走看看