请问:怎么把数据库中的数据加载到下拉列表中?很有难度哦。^o^

xatop 2003-10-18 03:52:15
请问:怎么把数据库中的数据加载到下拉列表中?很有难度哦。^o^
...全文
63 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ddangerous169 2003-10-18
  • 打赏
  • 举报
回复
我上层两兄弟提出了两种可行的访法。我再来一种。用dataReader
dim con as ..
dim com as ..
dim dr as datareader
..
dr=com.excuteReader()
while(dr.read())
ddlName.add(dr(""),dr(""))
wend
..
xu0923 2003-10-18
  • 打赏
  • 举报
回复
ddlName.DataSource = dataRead();//指定数据源为DATAREADER
ddlName.DataValueField = "后台操纵的值";
ddlName.DataTextField = "实际显示的值";
ddlName.DataBind()
helloqiqi 2003-10-18
  • 打赏
  • 举报
回复
DataTable table=new DataTable();
Adapter.Fill(table);
for(int i=0;o<table.Columns.Count;i++)
{Dropdownlist1.Items.Add(new ListItem(table.Columns[i].Caption);)}
就可以将表中的字段显示到dropdownlist中了
rgbcn 2003-10-18
  • 打赏
  • 举报
回复

Populate Dropdown List-box from a database using ASP.NET Server Control


DropDownList Server Control using the SQLClient class against SQL Server and using a datareader

<%@ Page Language="VB" EnableSessionState="False" EnableViewState="False" debug="False" trace="False" strict="True" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<script language=VB runat=server>

Sub Page_Load(Sender As Object, E As EventArgs)
Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs"))
Dim mySQL as string = "Select au_id, au_lname, au_fname from Authors"
Dim myCmd as New SQLCommand(mySQL,myConnection)
myConnection.Open()

MyDropDownList.DataSource = myCmd.ExecuteReader()
myDropDownList.DataBind()
myConnection.Close()
End Sub

</script>
<html>
<head><title>DropListBox using SQLDataReader</title></head>

<body>
<asp:dropdownlist id="MyDropDownList" DataValueField = "au_lname" DataTextField = "au_id" runat="server"/>
</body>
</html>

DropDownList Server Control using the OleDb class against Access db and using a datareader
<%@ Page Language="VB" EnableSessionState="False" EnableViewState="False" Trace="False" Debug="False" Strict="True" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)

Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs"))
Dim mySQL as string = "SELECT ProductID, productName FROM products"
Dim myCmd as New OLEDBCommand(mySQL,myConnection)
myConnection.Open()

myDropDown.DataSource = myCmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
myDropDown.DataBind()
myConnection.Close()
End Sub
</script>
<html>
<head><title>Dropdown List box using OLEDB</Title></head>
<body>
<asp:dropdownlist id="myDropDown" datatextfield="ProductName" dataTextValue="Productid" runat="server" />
</body>
</html>

DropDownList Server Control using the OleDb class against Access db and using a SQLDataAdapter

<%@ Page EnableSessionState="False" EnableViewState="False" debug="False" trace="False" strict="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>

<script language=VB runat=server>

Sub Page_Load(Sender As Object, E As EventArgs)

Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs"))
Dim ds as DataSet = new DataSet()
dim adapter as SqlDataAdapter = new SqlDataAdapter("Select au_id, au_lname, au_fname from Authors", myConnection)
adapter.Fill(ds,"Authors")

MyDropDownList.DataSource = ds.Tables("Authors").DefaultView

'This is what will be displayed in the dropdown box
MyDropDownList.DataTextField = "au_id"

'This is what will be the Value sent in the form values
MyDropDownList.DataValueField = "au_lname"

'This binds the data to the dropdownlist control.
MyDropDownList.DataBind()

End Sub

</script>
<html><head></head>
<body>

<asp:dropdownlist id="MyDropDownList" runat="server"/>
</body></html>

Web.Config file that stores the connection string

<configuration>
<appSettings>
<add key="DSN_northwind" value="DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\inetpub\wwwroot\nwind.mdb"/>
<add key="DSN_pubs" value="server=(local)\NetSDK;uid=sa;pwd=;database=pubs" />
</appSettings>
<system.web>
<customErrors mode="Off" />
</system.web>
</configuration>

--------------------------------------------------------------------------------













huangwangman 2003-10-18
  • 打赏
  • 举报
回复
一下是我写的一段代码,你可以参考一下:

ddlUnit.DataSource = ds.Tables("Unit").DefaultView
ddlUnit.DataValueField = "单位代码"
ddlUnit.DataTextField = "单位名称"
ddlUnit.DataBind()
dahuzizyd 2003-10-18
  • 打赏
  • 举报
回复
看看帮助里关于Droplistbox类的成员列表,里面有两个属性,一个是用来显示的,一个是用来填充下拉列表的,我在网吧,没办法说的更细,你可以找找看
xatop 2003-10-18
  • 打赏
  • 举报
回复
出来乍到,没有看到各位高手在此,抱歉!-_-
jinye 2003-10-18
  • 打赏
  • 举报
回复
同意楼上的.
skyover 2003-10-18
  • 打赏
  • 举报
回复
这也很有难度?汗。。。

不管是用DataSource还是用DataReader遍历都可以做到的。

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧