首页 > 代码库 > ado:SqlDataAdapter,dataset 与SqlDataReader的用法一

ado:SqlDataAdapter,dataset 与SqlDataReader的用法一

原文发布时间为:2008-08-01 —— 来源于本人的百度文章 [由搬家工具导入]

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection pubsConn = new SqlConnection("Server=.\\SQLEXPRESS;Database=pubs;Integrated Security=true");
        //SqlConnection pubsConn = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConn"].ConnectionString);

        //pubsConn.Open();

        SqlDataAdapter sda = new SqlDataAdapter("SELECT TOP(10) * FROM authors", pubsConn);
        DataSet ds = new DataSet();
        sda.Fill(ds, "pubauthor");

        GridView1.DataSource = ds.Tables["pubauthor"];
        GridView1.DataBind();

        foreach (DataRow dr in ds.Tables["pubauthor"].Rows)//从dataset表中获取某一列的所有值
        {
            ListBox1.Items.Add(dr["au_lname"].ToString());
        }

        pubsConn.Close();
      
        SqlCommand sqlcmd=new SqlCommand("SELECT * FROM authors",pubsConn);
        pubsConn.Open();
        SqlDataReader sdr=sqlcmd.ExecuteReader();

        if(sdr.HasRows)
        {
            while(sdr.Read())
            {
                DropDownList1.Items.Add(sdr["au_lname"].ToString());
                //DropDownList1.Items.Add(sdr[1].ToString());
                //ListItem item = new ListItem(sdr.GetString(1), sdr.GetString(1));
                //DropDownList1.Items.Add(item);
            }
          
        }
        sdr.Close();
        pubsConn.Close();
    }
}

ado:SqlDataAdapter,dataset 与SqlDataReader的用法一