using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Web;
using System.Data.OleDb; 

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int flag = -1, flag2 = 0;
        DataTable dt;
        string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb";
        string filename, filename1, tempfn = "";

        OleDbConnection conn;
        public Form1()
        {
            InitializeComponent();
        }

        private void count()
        {
            this.dateTimePicker1.ValueChanged -= new System.EventHandler(this.dateTimePicker1_ValueChanged);
            this.textBox1.TextChanged -= new System.EventHandler(this.textBox1_TextChanged);
            this.textBox2.TextChanged -= new System.EventHandler(this.textBox2_TextChanged);
            this.textBox3.TextChanged -= new System.EventHandler(this.textBox3_TextChanged);
            this.textBox5.TextChanged -= new System.EventHandler(this.textBox5_TextChanged);
            this.textBox6.TextChanged -= new System.EventHandler(this.textBox6_TextChanged);
        }
        private void plus()
        {
            this.dateTimePicker1.ValueChanged += new System.EventHandler(this.dateTimePicker1_ValueChanged);
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
            this.textBox3.TextChanged += new System.EventHandler(this.textBox3_TextChanged);
            this.textBox5.TextChanged += new System.EventHandler(this.textBox5_TextChanged);
            this.textBox6.TextChanged += new System.EventHandler(this.textBox6_TextChanged);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (tempfn.Length != 0)
            {
                File.Delete(tempfn);
                tempfn = "";
            }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {

            this.Height = 450;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            count();
            this.Location = new Point(400, 200);
            textBoxTips.Text = "输入信息即可查询,输入所有信息便可添加";
            textBox1.Text = string.Empty;
            textBox2.Text = string.Empty;
            textBox3.Text = string.Empty;
            textBox5.Text = string.Empty;
            textBox6.Text = string.Empty;
            button4.Text = "点击以选择文件";
            this.Width = 600;
            this.Height = 450;
            this.panel1.Top = this.ClientSize.Height / 10;
            this.panel1.Left = this.ClientSize.Width / 5;
            this.dateTimePicker1.Format = DateTimePickerFormat.Short;
            this.dateTimePicker1.CustomFormat = null;
            this.dateTimePicker1.Checked = false;
            if (flag == -1)
            {
                conn = new OleDbConnection(connStr);
                conn.Open();
            }
            functreeview();
            button1.Hide();
            button2.Hide();
            button3.Hide();
            button7.Hide();
            panel2.Hide();
            panel3.Hide();
            listBox1.Hide();
            listBox2.Hide();
            flag = 0;
            plus();
        }

        private int inTree(TreeView t, string s)
        {
            int intree = -1;
            for (int i = 0; i < t.Nodes.Count; i++)
            {
                if (s == t.Nodes[i].Text)
                {
                    intree = i;
                    break;
                }
            }
            return intree;
        }

        private void functreeview()
        {
            treeView1.Nodes.Clear();
            string selectCmd;

            OleDbDataAdapter myAdapter;

            selectCmd =
                "SELECT 作者,论文题目,发表期刊,发表日期,卷号,论文类别 FROM 论文 WHERE true ";

            myAdapter = new OleDbDataAdapter(selectCmd, conn);
            DataSet myDataSet = new DataSet();
            myAdapter.Fill(myDataSet, "mytable");

            dt = myDataSet.Tables["mytable"];

            treeView1.Nodes.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TreeNode node0 = new TreeNode();

                node0.Text = "论文题目：" + dt.Rows[i]["论文题目"].ToString() + "|";
                node0.Text += "发表期刊：" + dt.Rows[i]["发表期刊"].ToString() + "|";
                node0.Text += "发表日期：" + dt.Rows[i]["发表日期"].ToString().Replace("0:00:00", "") + "|";
                node0.Text += "卷号：" + dt.Rows[i]["卷号"].ToString() + "|";
                node0.Text += "论文类别：" + dt.Rows[i]["论文类别"].ToString() + "|";
                string treeviewp = dt.Rows[i]["作者"].ToString();

                int idx = inTree(treeView1, treeviewp);

                if (treeView1.Nodes.Count == 0 || idx < 0)
                {
                    TreeNode node1 = new TreeNode();
                    node1.Text = dt.Rows[i]["作者"].ToString();
                    treeView1.Nodes.Add(node1);
                    node1.Nodes.Add(node0);
                }
                else
                {
                    treeView1.Nodes[idx].Nodes.Add(node0);
                }
            }
            treeView2.Nodes.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TreeNode node0 = new TreeNode();

                node0.Text = "作者：" + dt.Rows[i]["作者"].ToString() + "|";
                node0.Text += "发表期刊：" + dt.Rows[i]["发表期刊"].ToString() + "|";
                node0.Text += "发表日期：" + dt.Rows[i]["发表日期"].ToString().Replace("0:00:00", "") + "|";
                node0.Text += "卷号：" + dt.Rows[i]["卷号"].ToString() + "|";
                node0.Text += "论文类别：" + dt.Rows[i]["论文类别"].ToString() + "|";
                string treeviewp = dt.Rows[i]["论文题目"].ToString();

                int idx = inTree(treeView2, treeviewp);

                if (treeView2.Nodes.Count == 0 || idx < 0)
                {
                    TreeNode node2 = new TreeNode();
                    node2.Text = dt.Rows[i]["论文题目"].ToString();
                    treeView2.Nodes.Add(node2);
                    node2.Nodes.Add(node0);
                }
                else
                {
                    treeView2.Nodes[idx].Nodes.Add(node0);
                }
            }
            treeView3.Nodes.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TreeNode node0 = new TreeNode();

                node0.Text = "作者：" + dt.Rows[i]["作者"].ToString() + "|";
                node0.Text += "发表期刊：" + dt.Rows[i]["发表期刊"].ToString() + "|";
                node0.Text += "发表日期：" + dt.Rows[i]["发表日期"].ToString().Replace("0:00:00", "") + "|";
                node0.Text += "卷号：" + dt.Rows[i]["卷号"].ToString() + "|";
                node0.Text += "论文题目：" + dt.Rows[i]["论文题目"].ToString() + "|";
                string treeviewp = dt.Rows[i]["论文类别"].ToString();

                int idx = inTree(treeView3, treeviewp);

                if (treeView3.Nodes.Count == 0 || idx < 0)
                {
                    TreeNode node2 = new TreeNode();
                    node2.Text = dt.Rows[i]["论文类别"].ToString();
                    treeView3.Nodes.Add(node2);
                    node2.Nodes.Add(node0);
                }
                else
                {
                    treeView3.Nodes[idx].Nodes.Add(node0);
                }
            }
            treeView4.Nodes.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TreeNode node0 = new TreeNode();

                node0.Text = "作者：" + dt.Rows[i]["作者"].ToString() + "|";
                node0.Text += "发表期刊：" + dt.Rows[i]["发表期刊"].ToString() + "|";
                node0.Text += "论文题目：" + dt.Rows[i]["论文题目"].ToString().Replace("0:00:00", "") + "|";
                node0.Text += "卷号：" + dt.Rows[i]["卷号"].ToString() + "|";
                node0.Text += "论文类别：" + dt.Rows[i]["论文类别"].ToString() + "|";
                string treeviewp = dt.Rows[i]["发表日期"].ToString();

                int idx = inTree(treeView4, treeviewp);

                if (treeView4.Nodes.Count == 0 || idx < 0)
                {
                    TreeNode node2 = new TreeNode();
                    node2.Text = dt.Rows[i]["发表日期"].ToString();
                    treeView4.Nodes.Add(node2);
                    node2.Nodes.Add(node0);
                }
                else
                {
                    treeView4.Nodes[idx].Nodes.Add(node0);
                }
            }
            treeView5.Nodes.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TreeNode node0 = new TreeNode();

                node0.Text = "作者：" + dt.Rows[i]["作者"].ToString() + "|";
                node0.Text += "发表日期：" + dt.Rows[i]["发表日期"].ToString() + "|";
                node0.Text += "论文题目：" + dt.Rows[i]["论文题目"].ToString().Replace("0:00:00", "") + "|";
                node0.Text += "卷号：" + dt.Rows[i]["卷号"].ToString() + "|";
                node0.Text += "论文类别：" + dt.Rows[i]["论文类别"].ToString() + "|";
                string treeviewp = dt.Rows[i]["发表期刊"].ToString();

                int idx = inTree(treeView5, treeviewp);

                if (treeView5.Nodes.Count == 0 || idx < 0)
                {
                    TreeNode node2 = new TreeNode();
                    node2.Text = dt.Rows[i]["发表期刊"].ToString();
                    treeView5.Nodes.Add(node2);
                    node2.Nodes.Add(node0);
                }
                else
                {
                    treeView5.Nodes[idx].Nodes.Add(node0);
                }
            }
        }

        private void funcpanel1m()//实现移动功能
        {
            while (panel1.Left > 40)
            {
                for (int i = 0; i < 400; i++) ;
                panel1.Left--;
            }
        }

        private bool funcdata()//实现查询功能
        {
            button2.Hide();
            button3.Hide();
            button7.Hide();
            if (tempfn.Length != 0)
            {
                File.Delete(tempfn);
                tempfn = "";
            }
            int x = 400;
            while (this.Width <= 920&&x>=240)
            {
                this.Width+=20;
                x -= 10;           
                this.Location = new Point(x, 200);
            }
            panel2.Top = panel2.Top;
            panel2.Left = panel1.Left + panel1.Width + 10;
            panel2.Show();
            DataView dv = dt.DefaultView;
            string selectName = "";
            if (textBox1.Text.Length > 0)
                selectName += "作者 like '%" + textBox1.Text + "%' AND ";
            if (textBox2.Text.Length > 0)
                selectName += "论文题目 like '%" + textBox2.Text + "%' AND ";
            if (textBox3.Text.Length > 0)
                selectName += "发表期刊 like '%" + textBox3.Text + "%' AND ";
            if (this.dateTimePicker1.Checked == true)
                selectName += " 发表日期 = #" + dateTimePicker1.Text + "#" + "AND ";
            if (textBox5.Text.Length > 0)
                selectName += "卷号 like '%" + textBox5.Text + "%' AND ";
            if (textBox6.Text.Length > 0)
                selectName += "论文类别 like '%" + textBox6.Text + "%' AND ";
            selectName += true;
            dv.RowFilter = selectName;
            dataGridView1.DataSource = dt;
            if (dataGridView1.RowCount > 1)
            {

                textBoxTips.Text = "双击表格中的数据即可打开文件"; 
                return true;

            }
            else return false;
        }

        private void funcshowbt1()
        {
            if (textBox1.TextLength * textBox2.TextLength * textBox3.TextLength * textBox5.TextLength * textBox6.TextLength * flag2 > 0 && dateTimePicker1.Checked == true)
            {
                flag = 2;
                button1.Show();
                flag2 = 0;                    
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
       {
            if (flag == 0 || flag == 1)
            { flag = 1; funcpanel1m(); }
            if (flag == 1)
            {
                if (funcdata()) button1.Hide();
                else funcshowbt1();
            }
       }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

            if (flag == 0 || flag == 1)
            { flag = 1; funcpanel1m(); }
            if (flag == 1)
            {
                if (funcdata()) button1.Hide();
                else funcshowbt1();
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            if (this.dateTimePicker1.Checked == true)
            {

                if (flag == 0 || flag == 1)
                { flag = 1; funcpanel1m(); }
                if (flag == 1)
                {
                    if (funcdata()) button1.Hide();
                    else funcshowbt1();
                }
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (flag == 0 || flag == 1)
            { flag = 1; funcpanel1m(); }
            if (flag == 1)
            {
                if (funcdata()) button1.Hide();
                else funcshowbt1();


                string selectCmd;
                OleDbCommand cmd;
                OleDbDataReader myReader;
                if (textBox3.Text.Length != 0)
                {
                    selectCmd =
                       "SELECT 刊名,刊名拼音 FROM 期刊 WHERE 刊名 like '%" + textBox3.Text + "%' OR 刊名拼音 like '%" + textBox3.Text + "%' ";
                    cmd = new OleDbCommand(selectCmd, conn);
                    myReader = cmd.ExecuteReader();

                    if (myReader.Read())
                    {
                        listBox1.Items.Clear();
                        listBox1.Top = textBox3.Top + textBox3.Height;
                        listBox1.Left = textBox3.Left;
                        listBox1.Width = textBox3.Width;
                        listBox1.Show();
                        this.listBox1.Items.Add(myReader["刊名"].ToString());
                    }
                    while (myReader.Read())
                    {
                        this.listBox1.Items.Add(myReader["刊名"].ToString());
                    }
                    myReader.Close();
                    cmd.Cancel();
                }
                else
                    listBox1.Hide();
            }
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

            if (flag == 0 || flag == 1)
            { flag = 1; funcpanel1m(); }
            if (flag == 1)
            {
                if (funcdata()) button1.Hide();
                else funcshowbt1();
            }
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            if (textBox6.Text.Length == 0)
            {
                listBox2.Items.Clear();
                listBox2.Hide();
            }
            if (flag == 0 || flag == 1)
            { flag = 1; funcpanel1m(); }
            if (flag == 1)
            {
                if (funcdata()) button1.Hide();
                else funcshowbt1();
                string selectCmd;
                OleDbCommand cmd;
                OleDbDataReader myReader;
                if (textBox6.Text.Length != 0)
                {
                    selectCmd =
                       "SELECT 类别,类别拼音 FROM 论文类别 WHERE 类别拼音 like '%" + textBox6.Text + "%' OR 类别 like '%" + textBox6.Text + "%'";
                    cmd = new OleDbCommand(selectCmd, conn);
                    myReader = cmd.ExecuteReader();

                    if (myReader.Read())
                    {
                        listBox2.Items.Clear();
                        listBox2.Top = textBox6.Top + textBox6.Height;
                        listBox2.Left = textBox6.Left;
                        listBox2.Width = textBox6.Width;
                        listBox2.Show();
                        this.listBox2.Items.Add(myReader["类别"].ToString());
                    }
                    while (myReader.Read())
                    {
                        this.listBox2.Items.Add(myReader["类别"].ToString());
                    }
                    myReader.Close();
                    cmd.Cancel();
                }
                else
                    listBox2.Hide();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            count();
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                filename = dlg.FileName;
                button4.Text = System.IO.Path.GetFileName(dlg.FileName);
                flag2 = 1;
            }
            if (flag == 0 || flag == 1)
            { flag = 1; funcpanel1m(); }
            if (flag == 1)
            {
                if (funcdata()) button1.Hide();
                else funcshowbt1();
                return;
            }
            plus();
        }

        private void button1_Click(object sender, EventArgs e)//实现新增功能
        {
            count();
            textBoxTips.Text = "！！！请等待！！！";
            FileStream pFileStream = null;
            byte[] pReadByte;
            pFileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
            BinaryReader binReader = new BinaryReader(pFileStream);
            binReader.BaseStream.Seek(0, SeekOrigin.Begin);
            pReadByte = binReader.ReadBytes((int)binReader.BaseStream.Length);
            pFileStream.Close();

            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;
            cmd.CommandText = "insert into 论文 values('" + textBox1.Text + "','"
                                                    + textBox2.Text + "','"
                                                    + textBox3.Text + "',#"
                                                    + dateTimePicker1.Text + "#,'"
                                                    + textBox5.Text + "','"
                                                    + textBox6.Text + "'," + "@olevalue)";
            OleDbParameter olep = new OleDbParameter("@olevalue", OleDbType.LongVarBinary, pReadByte.Length);
            olep.Value = pReadByte;
            cmd.Parameters.Add(olep);
            cmd.ExecuteNonQuery();

            button4.Text = "请点击以添加文件";
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            dateTimePicker1.Checked = false;
            textBox5.Clear();
            textBox6.Clear();
            button1.Hide();
            functreeview();
            funcdata();
            textBoxTips.Text = "！！！添加成功！！！";
            plus();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            count();
            TreeNode selNode = treeView1.SelectedNode;
            if (selNode.Parent == null) return;
            if (tempfn.Length != 0)
            {
                File.Delete(tempfn);
                tempfn = "";
            }            
            flag = 3;
            string selectName = selNode.Text.Replace("：", " = '").Replace("|", "' AND ").Replace("发表日期 = '", "发表日期 = # ").Replace("' AND 卷号", "# AND 卷号");
            string selectCmd;
            OleDbCommand cmd;
            OleDbDataReader myReader;

            selectCmd =
                "SELECT * FROM 论文 WHERE  " + selectName + "true";
            cmd = new OleDbCommand(selectCmd, conn);
            myReader = cmd.ExecuteReader();
            
            if (myReader.Read())
            {
                textBox1.Text = myReader["作者"].ToString();
                textBox2.Text = myReader["论文题目"].ToString();
                textBox3.Text = myReader["发表期刊"].ToString();
                dateTimePicker1.Text = myReader["发表日期"].ToString();
                dateTimePicker1.Checked = true;
                textBox5.Text = myReader["卷号"].ToString();
                textBox6.Text = myReader["论文类别"].ToString();
                button4.Text = myReader["论文题目"].ToString()+".pdf";
            }
            myReader.Close();
            cmd.Cancel();
            panel2.Hide();
            while (this.Width >600)
            {
                this.Width -= 20;
            }
            this.panel1.Left = this.ClientSize.Width / 5;
            button2.Left = panel1.Left - 50;
            button3.Left = button2.Left + 100;
            button7.Left = button3.Left + 100;
            button2.Show();
            button3.Show();
            button7.Show();
            flag = 1;
            plus();
        }

        private void 
        dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            count();
            string temp;
            textBox1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            textBox2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            textBox3.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            temp = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            textBox5.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            textBox6.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            button4.Text = textBox2.Text + ".pdf";
            dateTimePicker1.Text = temp;
            dateTimePicker1.Checked = true;
            panel2.Hide();
            this.Width = 600;
            this.panel1.Left = this.ClientSize.Width / 5;            
            if (tempfn.Length != 0)
            {
                File.Delete(tempfn);
                tempfn = "";
            }
            tempfn = button4.Text;
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select 正文 from 论文 where 论文题目 = '" + dataGridView1.CurrentRow.Cells[1].Value + "'";
            OleDbDataReader dbread = cmd.ExecuteReader();
            dbread.Read();
            byte[] pReadByte = (byte[])dbread[0];

            MemoryStream stream = new MemoryStream(pReadByte);
            FileStream pFileStream = new FileStream(tempfn, FileMode.OpenOrCreate);
            pFileStream.Write(pReadByte, 0, pReadByte.Length);
            pFileStream.Close();
            System.Diagnostics.Process.Start(tempfn);
            button2.Show();
            button3.Show();
            flag = 1;
            plus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (flag == 1)
            {
                flag = 3;
                panel1.Left = 40;
                panel3.Top = panel1.Top;
                panel3.Left = panel1.Left;
                panel3.Show();
                while (panel3.Left < panel1.Left + panel1.Width)
                {
                    for (int i = 0; i < 1000; i++) ;
                    panel3.Left++;
                }
                button2.Text = "确认修改";
                button3.Text = "返回上一级菜单";
                return;
            }
            if (flag == 3)
            {
                FileStream pFileStream = null;
                byte[] pReadByte = null;
                if (filename1 != null)
                {
                    pFileStream = new FileStream(filename1, FileMode.Open, FileAccess.Read);
                    BinaryReader binReader = new BinaryReader(pFileStream);
                    binReader.BaseStream.Seek(0, SeekOrigin.Begin);
                    pReadByte = binReader.ReadBytes((int)binReader.BaseStream.Length);
                    pFileStream.Close();
                }

                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = conn;
                string selectName = "";
                selectName += " 作者 = '" + textBox1.Text + "'" + "AND ";
                selectName += " 论文题目 = '" + textBox2.Text + "'" + "AND ";
                selectName += " 发表期刊 = '" + textBox3.Text + "'" + "AND ";
                selectName += " 发表日期 = #" + dateTimePicker1.Text + "#" + "AND ";
                selectName += " 论文类别 = '" + textBox6.Text + "'" + "AND ";
                selectName += true;

                string selectC = "";

                int num = 0;

                if (textBox11.Text.Length != 0)
                {
                    num++;
                    selectC += " 作者 = '" + textBox11.Text + "'";
                }
                if (textBox12.Text.Length != 0)
                {
                    if (num > 0) selectC += " , ";
                    num++;
                    selectC += " 论文题目 = '" + textBox12.Text + "'";
                }
                if (textBox13.Text.Length != 0)
                {
                    if (num > 0) selectC += " , ";
                    num++;
                    selectC += " 发表期刊 = '" + textBox13.Text + "'";
                }
                if (dateTimePicker2.Checked == true)
                {
                    if (num > 0) selectC += " , ";
                    num++;
                    selectC += " 发表日期 = #" + dateTimePicker2.Text + "#";
                }
                if (textBox15.Text.Length != 0)
                {
                    if (num > 0) selectC += " , ";
                    num++;
                    selectC += " 卷号 = '" + textBox15.Text + "'";
                }
                if (textBox16.Text.Length != 0)
                {
                    if (num > 0) selectC += " , ";
                    num++;
                    selectC += " 论文类别 = '" + textBox16.Text + "'";
                }
                if (button5.Text != button5.Text)
                {
                    if (button5.Text != "请点击以添加文件")
                    {
                        num++;
                        selectC += " 正文 =  @olevalue ";
                    }
                }
                if (selectC.Length > 0)
                {
                    cmd.Connection = conn;
                    cmd.CommandText = "UPDATE 论文 SET " + selectC + " WHERE " + selectName;
                    if (filename1 != null)
                    {
                        OleDbParameter olep = new OleDbParameter("@olevalue", OleDbType.LongVarBinary, pReadByte.Length);
                        olep.Value = pReadByte;
                        cmd.Parameters.Add(olep);
                    }
                    cmd.ExecuteNonQuery();
                    button5.Text = "请点击以添加文件";
                    MessageBox.Show("！！！修改信息成功！！！");
                    flag = 1;
                    button2.Text = "修改";
                    button3.Text = "删除";
                    flag = 1;   
                    Form1_Load(null, null);

                }
                else
                {
                    MessageBox.Show("！！！不做任何修改！！！");
                }
            }
        }

        private void funcdetele()
        {
            if (textBox1.Text.Length * textBox2.Text.Length * textBox3.Text.Length * textBox5.Text.Length * textBox6.Text.Length > 0 && dateTimePicker1.Checked == true)
            {
                string selectName = "", selectCmd;
                selectName += " 作者 = '" + textBox1.Text + "'" + "AND ";
                selectName += " 论文题目 = '" + textBox2.Text + "'" + "AND ";
                selectName += " 发表期刊 = '" + textBox3.Text + "'" + "AND ";
                selectName += " 发表日期 = #" + dateTimePicker1.Text + "#" + "AND ";
                selectName += " 论文类别 = '" + textBox6.Text + "'" + "AND ";
                selectName += true;

                OleDbCommand cmd;
                OleDbDataReader myReader;

                selectCmd =
                    "SELECT * From 论文 where " + selectName + "";
                cmd = new OleDbCommand(selectCmd, conn);
                myReader = cmd.ExecuteReader();
                if (myReader.Read())
                {
                    if (MessageBox.Show("是否确认删除?", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        string s = selectName.Replace("AND True", "");
                        selectCmd =
                           "DELETE From 论文 where " + s + "";
                        cmd = new OleDbCommand(selectCmd, conn);
                        cmd.ExecuteNonQuery();
                        button4.Text = "请点击以添加文件";
                        MessageBox.Show("！！！删除成功！！！");
                        flag = 1;       
                        Form1_Load(null, null);

                    }
                }
                myReader.Close();
            }
            else
            {
                MessageBox.Show("条件不俱全，请输入要删除的数据的所有信息，并保证他是正确的");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (flag == 1)
            {
                funcdetele();
                return;
            }
            if (flag == 3)
            {
                if (MessageBox.Show("是否返回上一级菜单?", "返回上一级菜单", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    foreach (Control ctl in this.panel2.Controls)
                    {
                        if (ctl is TextBox)
                            ((TextBox)ctl).Text = string.Empty;
                    }
                    flag = 1;
                    panel3.Hide();
                    this.panel1.Left = this.ClientSize.Width / 5;
                    button2.Text = "修改";
                    button3.Text = "删除";
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                filename1 = dlg.FileName;
                button5.Text = System.IO.Path.GetFileName(dlg.FileName);
                if (button4.Text == button5.Text)
                    button5.Text = "请点击以添加文件";
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            count();
            textBox1.Text = string.Empty;
            textBox2.Text = string.Empty;
            textBox3.Text = string.Empty;
            textBox5.Text = string.Empty;
            textBox6.Text = string.Empty;
            button4.Text = "点击以选择文件";
            button5.Text = string.Empty;
            dateTimePicker1.Checked = false;
            dateTimePicker2.Checked = false;
            button2.Hide();
            button3.Hide();
            button7.Hide();
            plus();
        }

        private void listBox1_Click(object sender, EventArgs e)
        {
            textBox3.Text = listBox1.SelectedItem.ToString();
            listBox1.Hide();
        }

        private void listBox2_Click(object sender, EventArgs e)
        {
            textBox6.Text = listBox2.SelectedItem.ToString();
            listBox2.Hide();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
                button6_Click(null,null);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (tempfn.Length != 0)
            {
                File.Delete(tempfn);
                tempfn = "";
            }
            tempfn = button4.Text;
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select 正文 from 论文 where 论文题目 = '" + textBox2.Text + "'";
            OleDbDataReader dbread = cmd.ExecuteReader();
            dbread.Read();
            byte[] pReadByte = (byte[])dbread[0];

            MemoryStream stream = new MemoryStream(pReadByte);
            FileStream pFileStream = new FileStream(tempfn, FileMode.OpenOrCreate);
            pFileStream.Write(pReadByte, 0, pReadByte.Length);
            pFileStream.Close();
            System.Diagnostics.Process.Start(tempfn);
            flag = 1;
        }

        private void treeView5_AfterSelect(object sender, TreeViewEventArgs e)
        {
            count();
            TreeNode selNode = treeView5.SelectedNode;
            if (selNode.Parent == null) return;
            if (tempfn.Length != 0)
            {
                File.Delete(tempfn);
                tempfn = "";
            }
            flag = 3;
            string selectName = selNode.Text.Replace("：", " = '").Replace("|", "' AND ").Replace("发表日期 = '", "发表日期 = # ").Replace("0:00:00' AND", "# AND");
            string selectCmd;
            OleDbCommand cmd;
            OleDbDataReader myReader;

            selectCmd =
                "SELECT * FROM 论文 WHERE  " + selectName + "true";
            cmd = new OleDbCommand(selectCmd, conn);
            myReader = cmd.ExecuteReader();

            if (myReader.Read())
            {
                textBox1.Text = myReader["作者"].ToString();
                textBox2.Text = myReader["论文题目"].ToString();
                textBox3.Text = myReader["发表期刊"].ToString();
                dateTimePicker1.Text = myReader["发表日期"].ToString();
                dateTimePicker1.Checked = true;
                textBox5.Text = myReader["卷号"].ToString();
                textBox6.Text = myReader["论文类别"].ToString();
                button4.Text = myReader["论文题目"].ToString() + ".pdf";
            }
            myReader.Close();
            cmd.Cancel();
            panel2.Hide();
            while (this.Width > 600)
            {
                this.Width -= 20;
            }
            this.panel1.Left = this.ClientSize.Width / 5;
            button2.Left = panel1.Left - 50;
            button3.Left = button2.Left + 100;
            button7.Left = button3.Left + 100;
            button2.Show();
            button3.Show();
            button7.Show();
            flag = 1;
            plus();
        }

        private void treeView4_AfterSelect(object sender, TreeViewEventArgs e)
        {
            count();
            TreeNode selNode = treeView4.SelectedNode;
            if (selNode.Parent == null) return;
            if (tempfn.Length != 0)
            {
                File.Delete(tempfn);
                tempfn = "";
            }
            flag = 3;
            string selectName = selNode.Text.Replace("：", " = '").Replace("|", "' AND ");
            string selectCmd;
            OleDbCommand cmd;
            OleDbDataReader myReader;

            selectCmd =
                "SELECT * FROM 论文 WHERE  " + selectName + "true";
            cmd = new OleDbCommand(selectCmd, conn);
            myReader = cmd.ExecuteReader();

            if (myReader.Read())
            {
                textBox1.Text = myReader["作者"].ToString();
                textBox2.Text = myReader["论文题目"].ToString();
                textBox3.Text = myReader["发表期刊"].ToString();
                dateTimePicker1.Text = myReader["发表日期"].ToString();
                dateTimePicker1.Checked = true;
                textBox5.Text = myReader["卷号"].ToString();
                textBox6.Text = myReader["论文类别"].ToString();
                button4.Text = myReader["论文题目"].ToString() + ".pdf";
            }
            myReader.Close();
            cmd.Cancel();
            panel2.Hide();
            while (this.Width > 600)
            {
                this.Width -= 20;
            }
            this.panel1.Left = this.ClientSize.Width / 5;
            button2.Left = panel1.Left - 50;
            button3.Left = button2.Left + 100;
            button7.Left = button3.Left + 100;
            button2.Show();
            button3.Show();
            button7.Show();
            flag = 1;
            plus();
        }

        private void treeView3_AfterSelect(object sender, TreeViewEventArgs e)
        {
            count();
            TreeNode selNode = treeView3.SelectedNode;
            if (selNode.Parent == null) return;
            if (tempfn.Length != 0)
            {
                File.Delete(tempfn);
                tempfn = "";
            }
            flag = 3;
            string selectName = selNode.Text.Replace("：", " = '").Replace("|", "' AND ").Replace("发表日期 = '", "发表日期 = # ").Replace("' AND 卷号", "# AND 卷号");
            string selectCmd;
            OleDbCommand cmd;
            OleDbDataReader myReader;

            selectCmd =
                "SELECT * FROM 论文 WHERE  " + selectName + "true";
            cmd = new OleDbCommand(selectCmd, conn);
            myReader = cmd.ExecuteReader();

            if (myReader.Read())
            {
                textBox1.Text = myReader["作者"].ToString();
                textBox2.Text = myReader["论文题目"].ToString();
                textBox3.Text = myReader["发表期刊"].ToString();
                dateTimePicker1.Text = myReader["发表日期"].ToString();
                dateTimePicker1.Checked = true;
                textBox5.Text = myReader["卷号"].ToString();
                textBox6.Text = myReader["论文类别"].ToString();
                button4.Text = myReader["论文题目"].ToString() + ".pdf";
            }
            myReader.Close();
            cmd.Cancel();
            panel2.Hide();
            while (this.Width > 600)
            {
                this.Width -= 20;
            }
            this.panel1.Left = this.ClientSize.Width / 5;
            button2.Left = panel1.Left - 50;
            button3.Left = button2.Left + 100;
            button7.Left = button3.Left + 100;
            button2.Show();
            button3.Show();
            button7.Show();
            flag = 1;
            plus();
        }

        private void treeView2_AfterSelect(object sender, TreeViewEventArgs e)
        {
            count();
            TreeNode selNode = treeView2.SelectedNode;
            if (selNode.Parent == null) return;
            if (tempfn.Length != 0)
            {
                File.Delete(tempfn);
                tempfn = "";
            }
            flag = 3;
            string selectName = selNode.Text.Replace("：", " = '").Replace("|", "' AND ").Replace("发表日期 = '", "发表日期 = # ").Replace("' AND 卷号", "# AND 卷号");
            string selectCmd;
            OleDbCommand cmd;
            OleDbDataReader myReader;

            selectCmd =
                "SELECT * FROM 论文 WHERE  " + selectName + "true";
            cmd = new OleDbCommand(selectCmd, conn);
            myReader = cmd.ExecuteReader();

            if (myReader.Read())
            {
                textBox1.Text = myReader["作者"].ToString();
                textBox2.Text = myReader["论文题目"].ToString();
                textBox3.Text = myReader["发表期刊"].ToString();
                dateTimePicker1.Text = myReader["发表日期"].ToString();
                dateTimePicker1.Checked = true;
                textBox5.Text = myReader["卷号"].ToString();
                textBox6.Text = myReader["论文类别"].ToString();
                button4.Text = myReader["论文题目"].ToString() + ".pdf";
            }
            myReader.Close();
            cmd.Cancel();
            panel2.Hide();
            while (this.Width > 600)
            {
                this.Width -= 20;
            }
            this.panel1.Left = this.ClientSize.Width / 5;
            button2.Left = panel1.Left - 50;
            button3.Left = button2.Left + 100;
            button7.Left = button3.Left + 100;
            button2.Show();
            button3.Show();
            button7.Show();
            flag = 1;
            plus();
        }
    }
}
