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

电子发烧友网工程师

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

AppendChunk和GetChunk方法应用实例(VB)

2009-1-8 10:14

<p><font face="Verdana">本范例使用 AppendChunk 和 GetChunk 方法来用其他记录中的数据填充图像字段。</font></p>
<p><font face="Verdana">Public Sub AppendChunkX()</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp; Dim cnn1 As ADODB.Connection<br/>&nbsp;&nbsp;&nbsp; Dim rstPubInfo As ADODB.Recordset<br/>&nbsp;&nbsp;&nbsp; Dim strCnn As String<br/>&nbsp;&nbsp;&nbsp; Dim strPubID As String<br/>&nbsp;&nbsp;&nbsp; Dim strPRInfo As String<br/>&nbsp;&nbsp;&nbsp; Dim lngOffset As Long<br/>&nbsp;&nbsp;&nbsp; Dim lngLogoSize As Long<br/>&nbsp;&nbsp;&nbsp; Dim varLogo As Variant<br/>&nbsp;&nbsp;&nbsp; Dim varChunk As Variant<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Const conChunkSize = 100</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp; ' Open a connection.<br/>&nbsp;&nbsp;&nbsp; Set cnn1 = New ADODB.Connection<br/>&nbsp;&nbsp;&nbsp;&nbsp;&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; <br/>&nbsp;&nbsp;&nbsp; ' Open the pub_info table.<br/>&nbsp;&nbsp;&nbsp; Set rstPubInfo = New ADODB.Recordset<br/>&nbsp;&nbsp;&nbsp; rstPubInfo.CursorType = adOpenKeyset<br/>&nbsp;&nbsp;&nbsp; rstPubInfo.LockType = adLockOptimistic<br/>&nbsp;&nbsp;&nbsp; rstPubInfo.Open "pub_info", cnn1, , , adCmdTable<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ' Prompt for a logo to copy.<br/>&nbsp;&nbsp;&nbsp; strMsg = "Available logos are : " &amp; vbCr &amp; vbCr<br/>&nbsp;&nbsp;&nbsp; Do While Not rstPubInfo.EOF<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strMsg = strMsg &amp; rstPubInfo!pub_id &amp; vbCr &amp; _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Left(rstPubInfo!pr_info, InStr(rstPubInfo!pr_info, ",") - 1) &amp; _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vbCr &amp; vbCr<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rstPubInfo.MoveNext<br/>&nbsp;&nbsp;&nbsp; Loop<br/>&nbsp;&nbsp;&nbsp; strMsg = strMsg &amp; "Enter the ID of a logo to copy:"<br/>&nbsp;&nbsp;&nbsp; strPubID = InputBox(strMsg)<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ' Copy the logo to a variable in chunks.<br/>&nbsp;&nbsp;&nbsp; rstPubInfo.Filter = "pub_id = '" &amp; strPubID &amp; "'"<br/>&nbsp;&nbsp;&nbsp; lngLogoSize = rstPubInfo!logo.ActualSize<br/>&nbsp;&nbsp;&nbsp; Do While lngOffset &lt; lngLogoSize<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; varChunk = rstPubInfo!logo.GetChunk(conChunkSize)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; varLogo = varLogo &amp; varChunk<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lngOffset = lngOffset + conChunkSize<br/>&nbsp;&nbsp;&nbsp; Loop<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ' Get data from the user.<br/>&nbsp;&nbsp;&nbsp; strPubID = Trim(InputBox("Enter a new pub ID" &amp; _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " [must be &gt; 9899 &amp; &lt; 9999]:"))<br/>&nbsp;&nbsp;&nbsp; strPRInfo = Trim(InputBox("Enter descriptive text:"))</font></p>
<p><font face="Verdana">&nbsp;&nbsp; ' Add the new publisher to the publishers table to avoid<br/>&nbsp;&nbsp; ' getting an error due to foreign key constraint.<br/>&nbsp;&nbsp; cnn1.Execute "INSERT publishers(pub_id, pub_name) VALUES('" &amp; _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strPubID &amp; "','Your Test Publisher')"<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ' Add a new record, copying the logo in chunks.<br/>&nbsp;&nbsp;&nbsp; rstPubInfo.AddNew<br/>&nbsp;&nbsp;&nbsp; rstPubInfo!pub_id = strPubID<br/>&nbsp;&nbsp;&nbsp; rstPubInfo!pr_info = strPRInfo</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp; lngOffset = 0 ' Reset offset.<br/>&nbsp;&nbsp;&nbsp; Do While lngOffset &lt; lngLogoSize<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; varChunk = LeftB(RightB(varLogo, lngLogoSize - lngOffset), _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; conChunkSize)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rstPubInfo!logo.AppendChunk varChunk<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lngOffset = lngOffset + conChunkSize<br/>&nbsp;&nbsp;&nbsp; Loop<br/>&nbsp;&nbsp;&nbsp; rstPubInfo.Update<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp; ' Show the newly added data.<br/>&nbsp;&nbsp;&nbsp; MsgBox "New record: " &amp; rstPubInfo!pub_id &amp; vbCr &amp; _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "Description: " &amp; rstPubInfo!pr_info &amp; vbCr &amp; _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "Logo size: " &amp; rstPubInfo!logo.ActualSize</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp; ' Delete new records because this is a demonstration.<br/>&nbsp;&nbsp;&nbsp; rstPubInfo.Requery<br/>&nbsp;&nbsp;&nbsp; cnn1.Execute "DELETE FROM pub_info " &amp; _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "WHERE pub_id = '" &amp; strPubID &amp; "'"</font></p>
<p><font face="Verdana">&nbsp;&nbsp; cnn1.Execute "DELETE FROM publishers " &amp; _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "WHERE pub_id = '" &amp; strPubID &amp; "'"</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp; rstPubInfo.Close<br/>&nbsp;&nbsp;&nbsp; cnn1.Close&nbsp;&nbsp;&nbsp; </font></p>
<p><font face="Verdana">End Sub<br/></font></p>

更多回帖

打开APP