<%
dim conn
set conn=CreateObject("ADODB.connection")
Const DatabaseServer="服务器IP"
Const DatabaseName="数据库名"
Const DataUserName="连接帐户"
Const DataPassword="连接密码"
conn.open "provider=sqloledbdata source="&DatabaseServer&"uid="&DataUserName&"pwd="&DataPassword&"database=" &DatabaseName
if err.number >0 then
response.Write("<center>连接数据库错误。</center>")
response.end()
end if
%>
ASP *** 作数据库的例子<%@ Page Language="VB"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.ADO" %>
<script language="VB" runat="server">
Sub Page_Load(Src as object, E as EventArgs)
If rblLTorGT.SelectedIndex = -1 Then rblLTorGT.SelectedIndex = 0
End Sub
Sub btnRunQuery_OnClick(Sender As Object, E As EventArgs)
Dim objConnection As ADOConnection
Dim objDSCommand As ADODataSetCommand
Dim objDataSet As DataSet
Dim strSQLQuery As String
strSQLQuery = "SELECT * FROM sample " &_
"WHERE (Sales " &rblLTorGT.SelectedItem.Value.ToString() &" " &
txtSalesAmount.Text.ToString() &") " &_
"ORDER BY " &ddlSortBy.SelectedItem.Value.ToString() &
ddlSortOrder.SelectedItem.Value.ToString()
objConnection = New ADOConnection("DBQ=" &Server.MapPath("database.mdb") &"Driver=
{Microsoft Access Driver (*.mdb)}")
objDSCommand = New ADODataSetCommand(strSQLQuery, objConnection)
objDataSet = New DataSet()
objDSCommand.FillDataSet(objDataSet, "sample")
lblSQLCommandLabel.Text = strSQLQuery
EmployeeDataGrid.DataSource = objDataSet.Tables("sample").DefaultView
EmployeeDataGrid.DataBind()
End Sub
</script>
<html>
<body>
<form action="database.aspx" method="post" runat="server">
<strong>Sales:</strong><br />
<table border="0">
<tr><td>
<asp:RadioButtonList id="rblLTorGT" runat="server">
<asp:ListItem value="<" runat="server">Less Than</asp:ListItem>
<asp:ListItem value=">" runat="server">Greater
Than</asp:ListItem>
</asp:RadioButtonList>
</td><td>
<asp:TextBox id="txtSalesAmount" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validSalesEntry" controlToValidate="txtSalesAmount"
errorMessage="You must enter a sales amount!"
display="dynamic"
/>
<asp:RangeValidator runat="server"
id="validSalesAmount" controlToValidate="txtSalesAmount"
type="Integer" minimumValue="-32000" maximumValue="+32000"
errorMessage="You must enter an integer!"
display="dynamic"
/>
</td></tr>
</table>
<strong>Sort By:</strong><br />
<asp:DropDownList id="ddlSortBy" runat="server">
<asp:ListItem value="id" >Id</asp:ListItem>
<asp:ListItem value="last_name" >Last Name</asp:ListItem>
<asp:ListItem value="first_name" >First Name</asp:ListItem>
<asp:ListItem value="sales">Sales</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList id="ddlSortOrder" runat="server">
<asp:ListItem value=" ASC" >Ascending</asp:ListItem>
<asp:ListItem value=" DESC">Descending</asp:ListItem>
</asp:DropDownList>
<asp:Button type="submit" id="btnRunQuery" text="Run Query" OnClick="btnRunQuery_OnClick"
runat="server" />
</form>
<p>
<strong>Results of:</strong><asp:label id="lblSQLCommandLabel" runat="server" />
</p>
<asp:DataGrid id="EmployeeDataGrid" HeaderStyle-Font-Bold="True" ToolTip="This is Cool!" runat="server"
maintainviewstate="false" />
</body>
</html>
VB用xmlhttp组件与asp进行数据交换即可。如果查询的参数比较简单(比如只有查询对象的编号),可以用GET方式发送请求;如果参数比较复杂(比如需要提供查询对象的规格、种类、价格、关键词等等),可以把这些参数构建为一个xml文档,然后通过POST方式发送。
服务器端的ASP程序接收到请求后,即可根据参数构建SQL查询语句查出所需数据,然后把查询结果用XML格式返回给VB客户端。
VB方接收到查询结果后,即可从XML对象中提取所需数据了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)