
Partial Class Manager_Tags_TableImportList
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Using mConn As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("XrenCRMConnString").ConnectionString)
                Dim mCMD As New Data.SqlClient.SqlCommand
                mCMD.CommandText = "select name from sysobjects where xtype='U'"
                Dim mRD As Data.SqlClient.SqlDataReader
                mConn.Open()
                mCMD.Connection = mConn
                mRD = mCMD.ExecuteReader
                rptList.DataSource = mRD
                rptList.DataBind()
            End Using
        End If
    End Sub

    Protected Sub cbSelectAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbSelectAll.CheckedChanged
        Dim chkBox As System.Web.UI.WebControls.CheckBox

        If cbSelectAll.Checked Then
            cbSelectAll.Text = "取消选择"
            For Each item As RepeaterItem In rptList.Items
                chkBox = CType(item.FindControl("CheckOn"), CheckBox)
                chkBox.Checked = True
            Next
        Else
            cbSelectAll.Text = "选择全部"
            For Each item As RepeaterItem In rptList.Items
                chkBox = CType(item.FindControl("CheckOn"), CheckBox)
                chkBox.Checked = False
            Next
        End If
    End Sub

    Protected Sub lnkBtnDel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkBtnDel.Click
        Dim chkBox As System.Web.UI.WebControls.CheckBox
        Dim TableNames() As String = Nothing
        For Each item As RepeaterItem In rptList.Items
            chkBox = CType(item.FindControl("CheckOn"), CheckBox)
            If chkBox.Checked Then
                If Not IsNothing(TableNames) Then
                    ReDim Preserve TableNames(UBound(TableNames) + 1)
                Else
                    ReDim TableNames(0)
                End If
                TableNames(UBound(TableNames)) = chkBox.ToolTip
            End If
        Next
        If Not IsNothing(TableNames) Then
            Using mConn As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("XrenCRMConnString").ConnectionString)
                mConn.Open()
                Dim mCMD As New Data.SqlClient.SqlCommand
                mCMD.Connection = mConn
                Dim tCount As Integer = 0
                Dim FieldSQL As StringBuilder
                Dim mRD As Data.SqlClient.SqlDataReader
                For Each t In TableNames
                    FieldSQL = New StringBuilder
                    mCMD.CommandText = "Select Count(ID) From TableData Where TableName = '" & t & "'"
                    Integer.TryParse(mCMD.ExecuteScalar.ToString, tCount)
                    If tCount > 0 Then
                        mCMD.CommandText = "Delete TableFields Where TableName ='" & t & "'"
                        mCMD.ExecuteNonQuery()
                    Else
                        mCMD.CommandText = "Insert into TableData (TableName,TableNote)VALUES('" & t & "','')"
                        mCMD.ExecuteNonQuery()
                    End If
                    mCMD.CommandText = "SELECT ColumnName=C.name,ColumnDesc=ISNULL(PFD.[value],N''),PrimaryKey=ISNULL(IDX.PrimaryKey,N'') FROM sys.columns C INNER JOIN sys.objects O ON C.[object_id]=O.[object_id] AND O.type='U' AND O.is_ms_shipped=0 INNER JOIN sys.types T ON C.user_type_id=T.user_type_id LEFT JOIN sys.default_constraints D ON C.[object_id]=D.parent_object_id AND C.column_id=D.parent_column_id AND C.default_object_id=D.[object_id] LEFT JOIN sys.extended_properties PFD ON PFD.class=1 AND C.[object_id]=PFD.major_id AND C.column_id=PFD.minor_id LEFT JOIN sys.extended_properties PTB ON PTB.class=1 AND PTB.minor_id=0 AND C.[object_id]=PTB.major_id LEFT JOIN (SELECT IDXC.[object_id],IDXC.column_id,Sort=CASE INDEXKEY_PROPERTY(IDXC.[object_id],IDXC.index_id,IDXC.index_column_id,'IsDescending') WHEN 1 THEN 'DESC' WHEN 0 THEN 'ASC' ELSE '' END,PrimaryKey=CASE WHEN IDX.is_primary_key=1 THEN N'1'ELSE N'0' END,IndexName=IDX.Name FROM sys.indexes IDX INNER JOIN sys.index_columns IDXC ON IDX.[object_id]=IDXC.[object_id] AND IDX.index_id=IDXC.index_id LEFT JOIN sys.key_constraints KC ON IDX.[object_id]=KC.[parent_object_id] AND IDX.index_id=KC.unique_index_id INNER JOIN (SELECT [object_id], Column_id, index_id=MIN(index_id) FROM sys.index_columns GROUP BY [object_id], Column_id) IDXCUQ ON IDXC.[object_id]=IDXCUQ.[object_id] AND IDXC.Column_id=IDXCUQ.Column_id AND IDXC.index_id=IDXCUQ.index_id) IDX ON C.[object_id]=IDX.[object_id] AND C.column_id=IDX.column_id " & _
                                        "WHERE O.name=N'" & t & "'  ORDER BY O.name,C.column_id"
                    mRD = mCMD.ExecuteReader
                    Do While mRD.Read
                        FieldSQL.Append("Insert into TableFields (TableName,FieldName,FieldNote,IsPrimaryKey)VALUES('" & t & "','" & mRD.GetValue(0).ToString & "','" & mRD.GetValue(1).ToString & "','" & mRD.GetValue(2).ToString & "');")
                    Loop
                    mRD.Close()
                    mCMD.CommandText = FieldSQL.ToString
                    mCMD.ExecuteNonQuery()
                Next
                mConn.Close()
            End Using
            IO.File.WriteAllText(Request.PhysicalApplicationPath & "CacheFile\TableData.txt", Now)
            IO.File.WriteAllText(Request.PhysicalApplicationPath & "CacheFile\TableFields.txt", Now)
        End If
        Page.ClientScript.RegisterClientScriptBlock(Page.GetType, "CloseWindow", "Windowclose();", True)
    End Sub
End Class
