dim ConnString as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MyDBPath;"
dim conn As System.Data.OleDb.OleDbConnection(ConnString)
dim cmd As New OleDb.OleDbCommand()
dim dr as OleDb.OleDbDataReader
cmd.connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select id, name from mytable"
try
conn.open
dr = cmd.executereader() 'assuming dr is your datareader
myDropDownList.datasource = dr
myDropDownList.DataTextField = "name"
myDropDownList.DataValueField = "id" 'example of assinging a value field too
myDropDownList.Databind()
finally
conn.close
end try
This is an example using a datareader for quick access and ideal for dropdownlists. You will see examples using datasets too but this just happens to be my preference from a performance point of view.
0 comments:
Post a Comment