发 帖  
原厂入驻New

AddNew 方法范例 (VBScript)

2009-1-8 10:31:40  2223
分享
<p><font face="Verdana">AddNew 方法范例 (VBScript)</font></p><font face="Verdana">
<p><br/>本范例使用 AddNew 方法来创建具有指定名称的新记录。</p>
<p><br/>在 Active Server Page (ASP) 中使用下列范例。要查看此完整功能范例,C:\mssdk\samples\dataaccess\rds 处必须存在数据源 AdvWorks.mdb(与 SDK 一起安装)。这是 microsoft Access 数据库文件。</p>
<p>使用 Find 定位文件 Adovbs.inc,并将其放到计划使用的目录中。请将下列代码剪切并粘贴到记事本或其他文本编辑器中,并将其另存为 AddNew.asp。可以在任何客户端浏览器中查看结果。</p>
<p>要执行本范例,请在 HTML 表中添加虚构的新记录。单击“Add New”。要删除不需要的记录,请参阅 Delete 方法范例。</p>
<p>&lt;%@Language = VBScript %&gt;<br/>&lt;!-- #Include file="ADOVBS.INC" --&gt;<br/>&lt;HTML&gt;<br/>&lt;HEAD&gt;<br/>&lt;TITLE&gt;ADO Open Method&lt;/TITLE&gt;<br/>&lt;STYLE&gt;<br/>&lt;!--<br/>TH {<br/>&nbsp;&nbsp;&nbsp; background-color: #008080; <br/>&nbsp;&nbsp;&nbsp; font-family: 'Arial Narrow','Arial',sans-serIF; <br/>&nbsp;&nbsp;&nbsp; font-size: xx-small;<br/>&nbsp;&nbsp;&nbsp; color: white;<br/>&nbsp;&nbsp;&nbsp; }<br/>TD { <br/>&nbsp;&nbsp;&nbsp; text-align: center;<br/>&nbsp;&nbsp;&nbsp; background-color: #f7efde;<br/>&nbsp;&nbsp;&nbsp; font-family: 'Arial Narrow','Arial',sans-serif; <br/>&nbsp;&nbsp;&nbsp; font-size: xx-small;<br/>&nbsp;&nbsp;&nbsp;&nbsp; }<br/>--&gt;<br/>&lt;/STYLE&gt;<br/>&lt;/HEAD&gt;<br/>&lt;BODY&gt; <br/>&lt;FONT FACE="MS SANS SERIF" SIZE=2&gt;<br/>&lt;Center&gt;&lt;H3&gt;ADO AddNew Method&lt;/H3&gt;<br/>&lt;!-- ADO Connection Object used to create recordset--&gt;<br/>&lt;% <br/>src = "C:\mssdk\samples\dataaccess\rds\advworks.mdb"<br/>sC &amp; src<br/>'Create and Open Connection Object<br/>Set OBJdbConn = Server.CreateObject("ADODB.Connection")<br/>OBJdbConn.Open&nbsp; sConnStr<br/>'Create and Open Recordset Object<br/>Set RsCustomerList = Server.CreateObject("ADODB.Recordset")<br/>RsCustomerList.ActiveConnection = OBJdbConn<br/>RsCustomerList.CursorType = adOpenKeyset<br/>RsCustomerList.LockType = adLockOptimistic<br/>RsCustomerList.Source = "Customers"<br/>RsCustomerList.Open<br/>%&gt;<br/>&lt;!—<br/>If this is first time page is open, Form collection will be empty when data is entered. <br/>run AddNew method<br/>--&gt;<br/>&lt;% If Not IsEmpty(Request.Form) Then<br/>&nbsp;&nbsp;&nbsp; If Not Request.Form("CompanyName") = "" Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RsCustomerList.AddNew<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RsCustomerList("CompanyName") = Request.Form("CompanyName")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RsCustomerList("ContactLastName") = Request.Form("LastName")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RsCustomerList("ContactFirstName") = Request.Form("FirstName")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RsCustomerList("PhoneNumber") = Request.Form("PhoneNumber")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RsCustomerList("City") = Request.Form("City")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RsCustomerList("StateOrProvince") = Request.Form("State")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RsCustomerList.Update<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RsCustomerList.MoveFirst<br/>&nbsp;&nbsp;&nbsp; End If<br/>End If<br/>%&gt;</p>
<p>&lt;TABLE COLSPAN=8 CELLPADDING=5 BORDER=0&gt;</p>
<p>&lt;!-- BEGIN column header row for Customer Table--&gt;</p>
<p>&lt;TR&gt;<br/>&lt;TD&gt;Company Name&lt;/TD&gt;<br/>&lt;TD&gt;Contact Name&lt;/TD&gt;<br/>&lt;TD&gt;Phone Number&lt;/TD&gt;<br/>&lt;TD&gt;City&lt;/TD&gt;<br/>&lt;TD&gt;State/Province&lt;/TD&gt;<br/>&lt;/TR&gt;<br/>&lt;!--display ADO Data from Customer Table <br/>one row on each pass through recordset--&gt;<br/>&lt;% Do While Not RsCustomerList.EOF %&gt;<br/>&lt;TR&gt;<br/>&nbsp; &lt;TD&gt; &lt;%= RSCustomerList("CompanyName")%&gt; &lt;/TD&gt;<br/>&nbsp; &lt;TD&gt; &lt;%= RScustomerList("ContactLastName") &amp; ", " %&gt; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%= RScustomerList("ContactFirstName") %&gt; &lt;/TD&gt;<br/>&nbsp; &lt;TD&gt; &lt;%= RScustomerList("PhoneNumber")%&gt; &lt;/TD&gt;<br/>&nbsp; &lt;TD&gt; &lt;%= RScustomerList("City")%&gt; &lt;/TD&gt;<br/>&nbsp; &lt;TD&gt; &lt;%= RScustomerList("StateOrProvince")%&gt; &lt;/TD&gt;<br/>&lt;/TR&gt; <br/>&lt;!-- Next Row = Record Loop and add to row html table--&gt;<br/>&lt;% <br/>&nbsp; RScustomerList.MoveNext <br/>Loop <br/>%&gt;<br/>&lt;/TABLE&gt; <br/>&lt;HR&gt;<br/>&lt;!-- Form to enter new record posts variables back to this page --&gt;<br/>&lt;Form Method=Post Action="AddNew.asp" Name=Form&gt;<br/>&lt;TABLE&gt;<br/>&lt;TR&gt;<br/>&lt;TD&gt;Company Name:&lt;/TD&gt;<br/>&lt;TD&gt;&lt;Input Type="Text" Size="50" Name="CompanyName" Value = ""&gt;&lt;/TD&gt;<br/>&lt;TR&gt;<br/>&lt;TD&gt;Contact First Name:&lt;/TD&gt;<br/>&lt;TD&gt;&lt;Input Type="Text" Size="50" Name="FirstName" Value = ""&gt;&lt;/TD&gt;<br/>&lt;TR&gt;<br/>&lt;TD&gt;Contact Last Name:&lt;/TD&gt;<br/>&lt;TD&gt;&lt;Input Type="Text" Size="50" Name="LastName" Value = ""&gt;&lt;/TD&gt;<br/>&lt;TR&gt;<br/>&lt;TD&gt;Contact Phone:&lt;/TD&gt;<br/>&lt;TD&gt;&lt;Input Type="Text" Size="50" Name="PhoneNumber" Value = ""&gt;&lt;/TD&gt;<br/>&lt;TR&gt;<br/>&lt;TD&gt;City:&lt;/TD&gt;<br/>&lt;TD&gt;&lt;Input Type="Text" Size="50" Name="City" Value = ""&gt;&lt;/TD&gt;<br/>&lt;TR&gt;<br/>&lt;TD&gt;State / Province:&lt;/TD&gt;<br/>&lt;TD&gt;&lt;Input Type="Text" Size="5" Name="State" Value = ""&gt;&lt;/TD&gt;<br/>&lt;TR&gt;<br/>&lt;TD&gt;&lt;Input Type="Submit" Value="Add New"&gt;<br/>&nbsp;&nbsp;&nbsp; &lt;Input Type="Reset" Value="Reset Form"&gt;<br/>&lt;/TABLE&gt;<br/>&lt;/Form&gt;<br/>&lt;%'Show location of data source<br/>Response.Write(OBJdbConn)<br/>%&gt;<br/>&lt;Script Language = "VBScript"&gt;<br/>Sub Form_OnSubmit<br/>&nbsp;&nbsp;&nbsp; MsgBox "Sending New Record to Server",,"ADO-ASP _Example"<br/>End Sub<br/>&lt;/Script&gt;<br/>&lt;/BODY&gt;<br/>&lt;/HTML&gt;</p>
<p></font>&nbsp;</p>
0
2009-1-8 10:31:40   评论 分享淘帖

只有小组成员才能发言,加入小组>>

135个成员聚集在这个小组

加入小组

创建小组步骤

关闭

站长推荐 上一条 /7 下一条

快速回复 返回顶部 返回列表