码迷,mamicode.com
首页 > 数据库 > 详细

ado:SqlDataAdapter,dataset 与SqlDataReader的用法一

时间:2017-07-09 16:19:05      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:tostring   文章   close   webpart   void   ons   apt   load   art   

原文发布时间为: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的用法一

标签:tostring   文章   close   webpart   void   ons   apt   load   art   

原文地址:http://www.cnblogs.com/handboy/p/7141591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!