asp.net dataReader数据访问对象dropdownlist
2014-10-14 00:00:00 by 【6yang】,
478
visits,
收藏 |
返回
Demo1.aspx.cs 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace test
{
public partial class demo1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = @"data source =.sqlexpress; initial catalog = test; persist security info=true; uid = sa; pwd=123456";
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("select * from admin", conn);
try
{
conn.Open();
SqlDataReader dataReader = command.ExecuteReader();
string graderName = "";
while (dataReader.Read())
{
graderName = (string)dataReader["user"];
this.ListBox1.Items.Add(graderName);
}
dataReader.Close();
}
catch (Exception ex)
{
Response.Write("<script>alert('操作数据出错')</script>");
Console.WriteLine(ex.Message);
}
finally {
conn.Close();
}
}
}
}
demo1.aspx 代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="demo1.aspx.cs" Inherits="test.demo1" %>
<!DOCTYPE html>
<html5>
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
user: <asp:DropDownList ID="ListBox1" runat="server"></asp:DropDownList>
</div>
</form>
</body>
</html5>
share