zoukankan      html  css  js  c++  java
  • DataReader、DataSet、DataView循环读取整表或单列

    代码
    string sql = "delete from Users where ID=3";
    SqlConnection conn
    = new SqlConnection(sqlConnString);
    SqlCommand cmd
    = new SqlCommand(sql, conn);
    try
    {
    conn.Open();
    sqldataReader dr
    =cmd.ExecutReader();

    while(dr.Reader())

    {

    responser.write(dr[
    "Name"]);

    }

    }
    catch (SqlException sqlex)
    {
    Response.Write(sqlex.Message
    + "<br>");
    }
    finally
    {
    conn.Close();
    }

    2、DataAdapter和DataSet、DataView读取单列数据

    string sql = "select * from Users";
    SqlConnection conn
    = new SqlConnection(sqlConnString);
    SqlDataAdapter da
    = new SqlDataAdapter(sql, conn);
    try
    {
    conn.Open();
    DataSet ds
    = new DataSet("Users");
    da.Fill(ds,
    "Users");
    DataView dv
    = ds.Tables[0].DefaultView;

    //DataView读取单列
    for (int i = 0; i < dv.Table.Rows.Count; i++)
    {
    Response.Write(dv.Table.Rows[i][
    "ID"].ToString()+" "+"<br>");
    }

    //DataSet读取单列
    foreach (DataRow row in ds.Tables[0].Rows)
    Response.Write(row[
    "Name"].ToString()+" "+row[0].ToString()+"<br>");
    DataList1.DataSource
    = dv;
    DataList1.DataBind();
    GridView1.DataSource
    = ds;
    GridView1.DataBind();
    }
    catch (SqlException exception)
    {
    Response.Write(exception.Message
    + "<br>");
    }
    finally
    {
    conn.Close();
    }

    循环读取dataset

    代码
    DataSet ds = new DataSet();

    foreach (DataTable dt in ds.Tables)
    {
    foreach (DataRow dr in dt.Rows)
    {
    foreach (DataColumn dc in dt.Columns)
    {
    System.Console.Write(dr[dc]);
    }
    }
    }

  • 相关阅读:
    try_files $uri $uri/ /index.php?$query_string;
    关于declare(strict_types=1)的有效范围
    SVN客户端安装与使用
    Java日志框架中真的需要判断log.isDebugEnabled()吗?
    Spring4自动装配(default-autowire)
    java的@PostConstruct注解
    Google Guava之--cache
    Java类加载机制与Tomcat类加载器架构
    搞懂JVM类加载机制
    Java 类加载机制
  • 原文地址:https://www.cnblogs.com/jayleke/p/1790847.html
Copyright © 2011-2022 走看看