我上层两兄弟提出了两种可行的访法。我再来一种。用dataReader
dim con as ..
dim com as ..
dim dr as datareader
..
dr=com.excuteReader()
while(dr.read())
ddlName.add(dr(""),dr(""))
wend
..
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>
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
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")