2012年6月10日 星期日

【vb語法】陣列,抓檔,比對

模組功能說明:
上傳圖片後,如果用GridView刪除資料庫路徑,但是又沒加寫刪除實體檔案,所以不會刪除到實體檔案,運用Arrary.Exists方法進行資料庫資料及實體資料陣列比對,比對後將沒有在資料庫內檔案進行刪除。



前端程式碼:

 <asp:FileUpload
            ID="FileUpload1" runat="server" />
&nbsp;<asp:Button ID="Button1" runat="server" Text="Send" />
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">HyperLink</asp:HyperLink>
    &nbsp;&nbsp;
        <asp:Button ID="Button2" runat="server" Text="陣列清除" />
        <asp:Button ID="Button3" runat="server" Text="BinarySearchDelete" />
    </p>
<p>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
            AllowSorting="True" AutoGenerateColumns="False" DataSourceID="sdDoc"
            BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
            CellPadding="4" DataKeyNames="index">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" />
                <asp:BoundField DataField="index" HeaderText="index" InsertVisible="False"
                    ReadOnly="True" SortExpression="index" />
                <asp:BoundField DataField="Url" HeaderText="Url" SortExpression="Url" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# Eval("Url") %>'
                            Text='<%# Eval("Url") %>'></asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
            <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
            <RowStyle BackColor="White" ForeColor="#330099" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
            <SortedAscendingCellStyle BackColor="#FEFCEB" />
            <SortedAscendingHeaderStyle BackColor="#AF0101" />
            <SortedDescendingCellStyle BackColor="#F6F0C0" />
            <SortedDescendingHeaderStyle BackColor="#7E0000" />
        </asp:GridView>
        <asp:SqlDataSource ID="sdDoc" runat="server"
            ConnectionString="<%$ ConnectionStrings:DBTestConn %>"
            DeleteCommand="DELETE FROM [T] WHERE [index] = @index"
            InsertCommand="INSERT INTO [T] ([Url]) VALUES (@Url)"
            SelectCommand="SELECT * FROM [T]"
            UpdateCommand="UPDATE [T] SET [Url] = @Url WHERE [index] = @index">
            <DeleteParameters>
                <asp:Parameter Name="index" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Url" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Url" Type="String" />
                <asp:Parameter Name="index" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:Label ID="Label1" runat="server" Text="Label" ForeColor="#996633"></asp:Label>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Label" ForeColor="#0066FF"></asp:Label>

Server端程式碼:
 
Imports System.Data.SqlClient
Imports System.IO

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim connstr As String = ConfigurationManager.ConnectionStrings("DBTestConn").ConnectionString
    Public conn As New SqlConnection(connstr)

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Dim s As String = Format(Now, "ddHHmmssfff").ToString & ".jpg"
        If FileUpload1.HasFile Then
            FileUpload1.SaveAs("C:\inetpub\wwwroot\PhotoWS\Doc\" & s)
        End If
        Dim cmd As New SqlCommand("INSERT INTO dbo.T (Url)VALUES (@Url)", conn)
        cmd.Parameters.Add("@Url", Data.SqlDbType.NVarChar, 200).Value = "~/Doc/" & s
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
    End Sub

    Dim key As String

    Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
        Dim cmd As New SqlCommand("SELECT Url FROM  T", conn)
        conn.Open()
        Dim dblist As String() = New String(50) {}
        Dim i As Integer = 0
        Try
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            Do While (dr.Read())
                dblist(i) = dr.Item("Url")
                i += +1
                If i > 50 Then
                    Exit Do
                End If
            Loop
        Catch ex As Exception
        Finally
            conn.Close()
        End Try
        i = 0

        Dim dirPath As DirectoryInfo = New DirectoryInfo("C:\inetpub\wwwroot\PhotoWS\Doc")
        Dim dirlist() As FileInfo = dirPath.GetFiles("*.jpg")
        Dim dirlistD As String() = New String(dirlist.Length - 1) {}
        For i = 0 To dblist.Length - 1
            Label1.Text &= CStr(dblist(i))
        Next
        For i = 0 To dirlist.Length - 1
            dirlistD(i) = "~/Doc/" & CStr(dirlist(i).ToString)
            Label2.Text &= dirlistD(i)
        Next
        For i = 0 To dirlistD.Length - 1
            key = dirlistD(i)
            If Array.Exists(dblist, AddressOf myCompareFunction) = False Then
                File.Delete("C:\inetpub\wwwroot\PhotoWS\" & dirlistD(i).Remove(0, 2))
            End If
        Next
    End Sub

    Private Function myCompareFunction(ByVal value As String) As Boolean
        If value = key Then
            Return True
        Else
            Return False
        End If
    End Function

End Class
---------------------------------------------------------------------------------
參考:
1.自己努力&網路&請益王老師

沒有留言:

張貼留言

如果久久沒有反應,請直接寄信
應該是我不太會用google blogger 導致有留言過久未處理><
實在深感抱歉..