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;
using System.Diagnostics;
namespace sjk
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string pTransPath = "";
private string path = "";
OpenFileDialog openFileDialog = new OpenFileDialog();
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click_1(object sender, EventArgs e)
{
openFileDialog.DefaultExt = "shp";
openFileDialog.Filter = "shp文件(*.shp)|*.shp";
path = textBox1.Text;
if (openFileDialog.ShowDialog() != DialogResult.Cancel)
{
pTransPath = openFileDialog.FileName;
textBox1.Text = openFileDialog.FileName;
}
}
private void button1_Click_1(object sender, EventArgs e)
{
pTransPath = textBox1.Text;
path = textBox1.Text + ".sql";
System.Diagnostics.Process p = new System.Diagnostics.Process();
if (radioButton1.Checked)
{
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.StandardInput.WriteLine(@" D:\PostgreSQL\9.3\bin\shp2pgsql.exe C:\Users\Administrator\Desktop\实验1\shapefiles\airports.shp > C:\Users\Administrator\Desktop\实验1\shapefiles\airports.sql ");
p.StandardInput.WriteLine("exit");
//string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
MessageBox.Show("转换成功", "转换提示");
}
else
{
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.StandardInput.WriteLine(@" D:\MySQL\Mysql\shp2mysql.exe C:\Users\Administrator\Desktop\实验1\shapefiles\airports.shp > C:\Users\Administrator\Desktop\实验1\shapefiles\airports.sql ");
p.StandardInput.WriteLine("exit");
//string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
MessageBox.Show("转换成功", "转换提示");
}
}
}
}
你好,针对你的问题,中文乱码问题,可能是由于数据源文件中的字符集和转换后的 SQL 文件的字符集不一致导致的。你可以尝试在命令行窗口中使用 -W 参数指定字符集,例如 -W "GB2312"。
另外,需要确保系统、数据库、数据库表和字段的字符集都相同,否则可能会出现乱码等问题。
同时,你的代码中似乎没有对读取的 shape 文件进行编码设置,你可以在读取 shape 文件时,使用相关的编码设置,例如:
StreamReader reader = new StreamReader(pTransPath, System.Text.Encoding.Default);
希望这些信息可以帮助你解决问题。