<%@ Page language="VB" Debug="false" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SQLClient" %> <script language="VB" runat="server"> Sub Page_Load(Source As Object, E As EventArgs) if Not Page.IsPostBack Then Dim oConn As SQLConnection Dim oComm As SQLCommand Dim oReader As SQLDataReader Dim sSQL As String Dim sConn As String sSQL= "SELECT au_id, au_fname + ' ' + au_lname AS AuthorName " sSQL+= "FROM Authors" sConn= "server=(local);database=pubs;Trusted_Connection=yes" oConn = New SQLConnection(sConn) oConn.Open() oComm = New SQLCommand(sSQL,oConn) oReader = oComm.ExecuteReader() oAuthors.DataSource = oReader oAuthors.DataBind() end if End SubSub Go_Click(Src As Object, E As EventArgs) SelectedItem.Text = "You picked " & oAuthors.SelectedItem.Text & " (" & _ oAuthors.SelectedItem.Value & ")." End Sub </script> <html> <head> <title>Populate A Drop-down List From A SQL Server Database</title> </head> <body> <form runat="server"> Please select an author: <asp:DropDownList ID="oAuthors" DataTextField="AuthorName" DataValueField="au_id" Runat="server" /> <asp:Button ID="Go" Text="Go" OnClick="Go_Click" Runat="server" /> <br /> <br /> <asp:Label ID="SelectedItem" Runat="server" /> </form> </body> </html>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
if Not Page.IsPostBack Then
Dim oConn As SQLConnection
Dim oComm As SQLCommand
Dim oReader As SQLDataReader
Dim sSQL As String
Dim sConn As String sSQL= "SELECT au_id, au_fname + ' ' + au_lname AS AuthorName "
sSQL+= "FROM Authors"
sConn= "server=(local);database=pubs;Trusted_Connection=yes" oConn = New SQLConnection(sConn)
oConn.Open() oComm = New SQLCommand(sSQL,oConn)
oReader = oComm.ExecuteReader() oAuthors.DataSource = oReader
oAuthors.DataBind()
end if
End SubSub Go_Click(Src As Object, E As EventArgs)
SelectedItem.Text = "You picked " & oAuthors.SelectedItem.Text & " (" & _
oAuthors.SelectedItem.Value & ")."
End Sub
</script>
<html>
<head>
<title>Populate A Drop-down List From A SQL Server Database</title>
</head>
<body>
<form runat="server">
Please select an author:
<asp:DropDownList ID="oAuthors" DataTextField="AuthorName"
DataValueField="au_id" Runat="server" />
<asp:Button ID="Go" Text="Go" OnClick="Go_Click" Runat="server" />
<br />
<br />
<asp:Label ID="SelectedItem" Runat="server" />
</form>
</body>
</html>
否则把Trusted_Connection=yes改为uid=用户名;pwd=密码;
代码如下:string sqlstring ="select name,age from table1 ";
SqlConnection MyConnection=new qlConnection("server=IP地址/实例名;database=数据库名称;Trusted_Connection=yes");
SqlCommand MyCommand = new SqlCommand(sqlstring, MyConnection);
MyConnection.Open();
SqlDataReader dr = MyCommand.ExecuteReader();
DropDownList4.DataSource = dr;
DropDownList4.DataTextField = "表中字段名称";
DropDownList4.DataBind();
MyConnection.Close();
祝你好运!
If Not IsPostBack Then
'数据绑定到DDL
End If否则由于回发而导致数据错误.
Me.DropDownList1.DataTextField = "Text"
Me.DropDownList1.DataValueField = "Value"