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");
}
}
}
calpuff-server-GeoFile.cs
最新推荐文章于 2021-05-02 01:01:17 发布