编程论坛
400万+工程师在用
华为|鸿蒙开发者日
直播报名
400万+工程师在用
华为|鸿蒙开发者日
直播报名

电子发烧友网工程师

17年用户 16436经验值
擅长:可编程逻辑 MEMS/传感技术 测量仪表 模拟技术 控制/MCU RF/无线
私信 关注

ActiveConnection、CommandText、CommandTimeout、CommandType、Size 和 Direction

2009-1-8 10:12

<font face="Verdana">
<p><br/>本范例使用 ActiveConnection、CommandText、CommandTimeout、CommandType、Size 和 Direction 属性来执行存储过程。</p>
<p>Public Sub ActiveConnectionX()</p>
<p>&nbsp;&nbsp;&nbsp; Dim cnn1 As ADODB.Connection<br/>&nbsp;&nbsp;&nbsp; Dim cmdByRoyalty As ADODB.Command<br/>&nbsp;&nbsp;&nbsp; Dim prmByRoyalty As ADODB.Parameter<br/>&nbsp;&nbsp;&nbsp; Dim rstByRoyalty As ADODB.Recordset<br/>&nbsp;&nbsp;&nbsp; Dim rstAuthors As ADODB.Recordset<br/>&nbsp;&nbsp;&nbsp; Dim intRoyalty As Integer<br/>&nbsp;&nbsp;&nbsp; Dim strAuthorID As String<br/>&nbsp;&nbsp;&nbsp; Dim strCnn As String</p>
<p>&nbsp;&nbsp;&nbsp; ' Define a command object for a stored procedure.<br/>&nbsp;&nbsp;&nbsp; Set cnn1 = New ADODB.Connection<br/>&nbsp;&nbsp;&nbsp; strCnn = "Provider=sqloLEDb;" &amp; _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "Data Source=srv;Initial Catalog=Pubs;User Id=sa;Password=; "<br/>&nbsp;&nbsp;&nbsp; cnn1.Open strCnn<br/>&nbsp;&nbsp;&nbsp; Set cmdByRoyalty = New ADODB.Command<br/>&nbsp;&nbsp;&nbsp; Set cmdByRoyalty.ActiveConnection = cnn1<br/>&nbsp;&nbsp;&nbsp; cmdByRoyalty.CommandText = "byroyalty"<br/>&nbsp;&nbsp;&nbsp; cmdByRoyalty.CommandType = adCmdStoredProc<br/>&nbsp;&nbsp;&nbsp; cmdByRoyalty.CommandTimeout = 15<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ' Define the stored procedure's input parameter.<br/>&nbsp;&nbsp;&nbsp; intRoyalty = Trim(InputBox( _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "Enter royalty:"))<br/>&nbsp;&nbsp;&nbsp; Set prmByRoyalty = New ADODB.Parameter<br/>&nbsp;&nbsp;&nbsp; prmByRoyalty.Type = adInteger<br/>&nbsp;&nbsp;&nbsp; prmByRoyalty.Size = 3<br/>&nbsp;&nbsp;&nbsp; prmByRoyalty.Direction = adParamInput<br/>&nbsp;&nbsp;&nbsp; prmByRoyalty.Value = intRoyalty<br/>&nbsp;&nbsp;&nbsp; cmdByRoyalty.Parameters.Append prmByRoyalty<br/>&nbsp; <br/>&nbsp;&nbsp;&nbsp; ' Create a recordset by executing the command.<br/>&nbsp;&nbsp;&nbsp; Set rstByRoyalty = cmdByRoyalty.Execute()<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ' Open the Authors table to get author names for display.<br/>&nbsp;&nbsp;&nbsp; Set rstAuthors = New ADODB.Recordset<br/>&nbsp;&nbsp;&nbsp; rstAuthors.Open "Authors", strCnn, , , adCmdTable<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ' Print current data in the recordset, adding<br/>&nbsp;&nbsp;&nbsp; ' author names from Authors table.<br/>&nbsp;&nbsp;&nbsp; debug.Print "Authors with " &amp; intRoyalty &amp; _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " percent royalty"<br/>&nbsp;&nbsp;&nbsp; Do While Not rstByRoyalty.EOF<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strAuthorID = rstByRoyalty!au_id<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Debug.Print , rstByRoyalty!au_id &amp; ", ";<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rstAuthors.Filter = "au_id = '" &amp; strAuthorID &amp; "'"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Debug.Print rstAuthors!au_fname &amp; " " &amp; _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rstAuthors!au_lname<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rstByRoyalty.MoveNext<br/>&nbsp;&nbsp;&nbsp; Loop</p>
<p>&nbsp;&nbsp;&nbsp; rstByRoyalty.Close<br/>&nbsp;&nbsp;&nbsp; rstAuthors.Close<br/>&nbsp;&nbsp;&nbsp; cnn1.Close<br/>&nbsp;&nbsp;&nbsp; <br/>End Sub</font></p>

更多回帖

打开APP