学习DataGrid控件的用法,使用该控件访问数据库。这里分享的是Vb.net版本。
C#版本的,请访问这里:C#编程之绑定数据到DataGrid控件 。
代码:
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Oledb" %>
<script language="vb" runat="server">
Sub Page_Load()
Dim strConnection as String = "Provider=Microsoft.Jet.OLEDB.4.0;"
strConnection += "Data Source=yourDatabase.mdb"
data_src.text = strConnection
Dim strSQL as string = "SELECT * FROM products;"
Dim objDataSet as new DataSet()
Dim objConnection as new OledbConnection(strConnection)
Dim objAdapter as new OledbDataAdapter(strSQL, objConnection)
objAdapter.Fill(objDataSet, "products")
Dim objDataView as New DataView(objDataSet.Tables("products"))
dgNameList.DataSource=objDataView
dgNameList.DataBind()
end Sub
</script>
<html>
<body>
<h4>从连接中读取数据:
<asp:label id="data_src" runat="server"/> to the DataGrid control.</h4>
<asp:datagrid id="dgNameList" runat="server" /><br />
</body>
</html>