calpuff-server-GeoFile.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


using System.IO;
namespace ServerV1._1
{
    public partial class GeoFile : Form
    {

        public static string terrelFileDir = @"C:\calpuff\terrel_file";
        public static string ctgprocFileDir = @"C:\calpuff\ctgproc_file";
        public static string[] ctgprocFiles;
        public static string[] terrelFiles;
        public GeoFile()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            folderBrowser.ShowDialog();

            if (folderBrowser.SelectedPath != "")
            {
                terrelFileDir = folderBrowser.SelectedPath;
                label5.Text = terrelFileDir;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            folderBrowser.ShowDialog();

            if (folderBrowser.SelectedPath != "")
            {
                ctgprocFileDir = folderBrowser.SelectedPath;
                label6.Text = ctgprocFileDir;
            }
        }

         //Refresh  刷新文件夹下所有的  并且存入数据库
        private void button1_Click(object sender, EventArgs e)
        {

            //OpenFileDialog dialog = new OpenFileDialog();
            
            //if(dialog.ShowDialog()==DialogResult.OK)
            //{
            //    terrel_file = dialog.FileName;
                
            //}
            textBox1.Text = "";

            terrelFiles = Directory.GetFiles(terrelFileDir);

            for(int i=0;i<terrelFiles.Length;i++)
            {
                textBox1.AppendText(terrelFiles[i] + "\r\n");
            }

        }


        // 第二个刷新操作
        private void button3_Click(object sender, EventArgs e)
        {
             textBox2.Text = "";
             ctgprocFiles = Directory.GetFiles(ctgprocFileDir);

            for (int i = 0; i < ctgprocFiles.Length; i++)
            {
                textBox2.AppendText(ctgprocFiles[i] + "\r\n");
            }
        }


        // 更新数据库
        private void button5_Click(object sender, EventArgs e)
        {
            //1 数据库连接

            //细节   两个目录不能相同

            if(terrelFileDir==ctgprocFileDir)
            {
                MessageBox.Show("The Two Directories Should Be Different");
                return;
            }

            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection();

            string connStr = "server=" + Form1.dbIp + ";user id=" + Form1.dbUser + ";password=" + Form1.dbPasswd + ";database=" + Form1.dbName + ";pooling=false;charset=utf8";

            conn.ConnectionString = connStr;

            try
            {
                conn.Open();
            }
            catch
            {
                MessageBox.Show("Database Connection Error");
                return;
            }
            
            if(terrelFiles==null)
            {
                MessageBox.Show("Please Refresh Terrel Files First");
                return;
            }

            if(ctgprocFiles==null)
            {
                MessageBox.Show("Please Refresh Ctgproc Files First");
                return;
            }

            
            // 开始更新--推到重来  原来的数据要清除
            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
            cmd.Connection = conn;

            cmd.CommandText = "truncate t_geo_file";

            cmd.ExecuteNonQuery();
            cmd.Dispose();//此处可以不用调用,  
            conn.Close();// 离开 using 块, connection 会自行关闭  
            // 更新terrel_file 的db
            for (int i = 0; i < terrelFiles.Length; i++)
            {

              
                // 关联新的cmd conn需要重启


                conn = new MySql.Data.MySqlClient.MySqlConnection();

                conn.ConnectionString = connStr;
                conn.Open();

                cmd = new MySql.Data.MySqlClient.MySqlCommand();
                cmd.Connection = conn;

                cmd.CommandText = "insert into t_geo_file(path,type) values(@0,@1)";
                cmd.Parameters.Add("@0", MySql.Data.MySqlClient.MySqlDbType.VarChar);
                cmd.Parameters.Add("@1", MySql.Data.MySqlClient.MySqlDbType.VarChar);
               

                cmd.Parameters[0].Value = terrelFiles[i];
                cmd.Parameters[1].Value = "terrel";
              
                cmd.ExecuteNonQuery();

                cmd.Dispose();//此处可以不用调用,  
                conn.Close();// 离开 using 块, connection 会自行关闭  

            }

           // 更新ctgproc 的DB
            for (int i = 0; i < ctgprocFiles.Length; i++)
            {


                conn = new MySql.Data.MySqlClient.MySqlConnection();

                conn.ConnectionString = connStr;
                conn.Open();

                cmd = new MySql.Data.MySqlClient.MySqlCommand();
                cmd.Connection = conn;

                cmd.CommandText = "insert into t_geo_file(path,type) values(@0,@1)";
                cmd.Parameters.Add("@0", MySql.Data.MySqlClient.MySqlDbType.VarChar);
                cmd.Parameters.Add("@1", MySql.Data.MySqlClient.MySqlDbType.VarChar);


                cmd.Parameters[0].Value = ctgprocFiles[i];
                cmd.Parameters[1].Value = "ctgproc";

               
                // 已经确认过可以连接了
                cmd.ExecuteNonQuery();

                cmd.Dispose();//此处可以不用调用,  
                conn.Close();// 离开 using 块, connection 会自行关闭  

            }

            MessageBox.Show("Database Updated Successfully");
        }

       
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值