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

  • 相关阅读:
    leetcode 78. 子集 JAVA
    leetcode 91. 解码方法 JAVA
    leetcode 75. 颜色分类 JAVA
    leetcode 74 搜索二维矩阵 java
    leetcode 84. 柱状图中最大的矩形 JAVA
    last occurance
    first occurance
    classical binary search
    LC.234.Palindrome Linked List
    LC.142. Linked List Cycle II
  • 原文地址:https://www.cnblogs.com/jayleke/p/1790847.html
Copyright © 2020-2023  润新知