Imports System.Data

Partial Class Start
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        If IsPostBack Then
            If String.IsNullOrEmpty(TextBoxName.Text) Then LabelMsg.Text = "名前が有りません" : Exit Sub
            If String.IsNullOrEmpty(TextBoxMail.Text) Then LabelMsg.Text = "メールが有りません" : Exit Sub
            'クッキーの書込み
            Dim zCookie As HttpCookie = New HttpCookie("Local")
            Dim zValue As String = "Class_1" & ":" & TextBoxMail.Text
            zCookie.Value = zValue
            zCookie.Expires = DateTime.Now.AddDays(365)
            zCookie.Path = "/"
            Response.Cookies.Add(zCookie)
            'クッキーの読出しとデータベースへの書込み
            If Request.Cookies("Local") IsNot Nothing Then
                Dim yCookie As String = Request.Cookies("Local").Value
                If yCookie = zValue Then
                    'データベースのリセット
                    resetDB("Member")
                    resetDB("admin")
                    resetDB("system")
                    Session("date") = Now.ToString
                    Session("name") = TextBoxName.Text
                    Session("mail") = TextBoxMail.Text
                    Session("IP") = Request.ServerVariables("REMOTE_ADDR")
                    Session("status") = "System"
                    Session("cookie") = zValue
                    SqlDataSourceMember.Insert()
                    SqlDataSourceAdmin.Insert()
                    SqlDataSourceSystem.Insert()


                    'siteMapの書き換え
                    Dim sr As New System.IO.StreamReader(Server.MapPath("Web.sitemap"), System.Text.Encoding.GetEncoding("UTF-8"))
                    Dim s As String = sr.ReadToEnd()
                    sr.Close()
                    Dim host As String = System.Net.Dns.GetHostName
                    s = Replace(s, "localhost", host)
                    'System.IO.Directory.Delete(Server.MapPath("Web.sitemap"))
                    Dim sw As New System.IO.StreamWriter(Server.MapPath("Web.sitemap"), False, System.Text.Encoding.GetEncoding("UTF-8"))
                    sw.Write(s)
                    sw.Close()

                    Response.Redirect("Delete.aspx") 'Debug時は、コメントアウトする
                Else
                    LabelMsg.Text = "ブラウザのクッキと内容が合いません" & yCookie
                    Exit Sub
                End If
            Else
                LabelMsg.Text = "ブラウザのクッキが有効になっていません"
                Exit Sub
            End If
        End If
    End Sub

    Protected Sub resetDB(ByVal x As String)
        Dim cn As New SqlClient.SqlConnection
        Dim cd As New SqlClient.SqlCommand
        Dim SQL As String

        'データベース接続
        cn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
        cn.Open()
        'SQL文生成
        SQL = "TRUNCATE TABLE " & x
        cd.CommandText = SQL
        cd.Connection = cn

        cd.ExecuteNonQuery()
        'クローズ解放
        cd.Dispose()
        cn.Close()
        cn.Dispose()
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If System.IO.File.Exists(Server.MapPath("delete.aspx")) Then
                Panel1.Visible = True
                Panel2.Visible = False
            Else
                Panel1.Visible = False
                Panel2.Visible = True
            End If

            '
            Try
                Dim dt As Data.DataView = SqlDataSourceMember.Select(DataSourceSelectArguments.Empty)

            Catch ex As Exception
                PanelMsg.Visible = True
                LabelMsg2.Text = "データベースが使えるようになっていません"
            End Try
        End If
    End Sub

    Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
        If IsPostBack Then
            'クッキーの消込
            Response.Cookies("Local").Value = ""
            Response.Cookies("Local").Expires = DateTime.Now.AddDays(-3)
            Response.Cookies("AccessDateTime").Value = DateTime.Now.ToString()
            LabelMsg.Text = "消去のコマンドを送りました。②に進んでください。"
        End If
    End Sub
End Class
