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]);
    }
    }
    }

  • 相关阅读:
    HZNU Personal Training 2
    6月30日-助教题解-刺客信条起源
    Educational Codeforces Round 90 (Rated for Div. 2)
    暑假python学习笔记
    HZNU Training 32 for Zhejiang Provincial Competition 2020
    哈夫曼树
    网络流24题
    splay
    生成树计数
    HZNU Training 29 for Zhejiang Provincial Competition 2020
  • 原文地址:https://www.cnblogs.com/jayleke/p/1790847.html
Copyright © 2011-2022 走看看