【发布时间】:2019-09-06 15:47:03
【问题描述】:
这就是我想要完成的事情......点击一个按钮,我想要:
- 将活动“users”表的现有数据复制到“old_users”表以进行备份
- 截断现有的“new_users”表以准备接受新行
- 解析 CSV 文件以将数据插入“new_users”表中
- 截断现有的“users”表,然后将“new_users”表的数据复制到“users”表中
我不是日常程序员,也很长时间没有编写程序了。但是我已经将一些代码拼凑在一起以获得一些可行的东西。以下是我当前的代码:
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 Microsoft.VisualBasic.FileIO;
using Renci.SshNet;
using Renci.SshNet.Common;
using MySql.Data.MySqlClient;
namespace ValidationImport
{
public partial class ValiationImport : Form
{
public ValiationImport()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnImportToDatabase_Click(object sender, EventArgs e)
{
try
{
using (var client = new SshClient("1.1.1.1", "[username]", "[password]")) // establishing ssh connection to server where MySql is hosted
{
client.Connect();
if (client.IsConnected)
{
var portForwarded = new ForwardedPortLocal("127.0.0.1", 3306, "127.0.0.1", 3306);
client.AddForwardedPort(portForwarded);
portForwarded.Start();
using (MySqlConnection con = new MySqlConnection("SERVER=localhost;PORT=3306;UID=[username];PASSWORD=[password];DATABASE=[dbname]")) // MySql database credentials
{
con.Open();
// Copying over the users table (with structure and indexes) to the old_users table. Truncating the new_users table to prepare for new data.
using (MySqlCommand cmd = new MySqlCommand("DROP TABLE test_old_users; CREATE TABLE test_old_users LIKE test_users; INSERT test_old_users SELECT * FROM test_users; TRUNCATE TABLE test_new_users;", con))
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
string fileName = "";
// Select the Validation file to import
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
fileName = dlg.FileName;
if (fileName != "")
{
using (TextFieldParser parser = new TextFieldParser(fileName))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters("|");
parser.ReadLine(); // Skip the first row of field title headers
while (!parser.EndOfData)
{
//Copy each row individually over to the MySql table
string[] row = parser.ReadFields();
using (MySqlCommand cmd = new MySqlCommand("INSERT INTO test_new_users (`Indv Id`, `Last Name`, `First Name`, `Middle Name`, `Birth date`, `Indv Addr Line 1`, `Indv Addr Line 2`, `Indv Addr Line 3`, `Indv City`, `Indv State`, `Indv Zip`, `Indv Country`, `Local Id`, `Local Name`, `Local User Id`, `Uniserv Id`, `Uniserv Name`, `Uniserv User Id`, `Chapter Name`, `Chapter User Id`, `Employer Id`, `Employer Name`, `Work Location Id`, `Work Location Name`, `Work Location User Id`, `Group Id`, `Group Name`, `Group Type Id`, `Group Type Name`, `SEA P01`, `Home Phone`, `Home Phone principal Ind`, `Home Phone Unlisted Ind`, `Mobile Phone`, `Mobile Phone Principal Ind`, `Mobile Phone Unlisted Ind`, `Home Email`, `Home Email Principal Ind`, `Work Email`, `Work Email Principal Ind`, `Other Email`, `Other Email Principal Ind`) VALUES (@IndvId, @LastName, @FirstName, @MiddleName, @BirthDate, @IndvAddrLine1, @IndvAddrLine2, @IndvAddrLine3, @IndvCity, @IndvState, @IndvZip, @IndvCountry, @LocalId, @LocalName, @LocalUserId, @UniservId, @UniservName, @UniservUserId, @ChapterName, @ChapterUserId, @EmployerId, @EmployerName, @WorkLocationId, @WorkLocationName, @WorkLocationUserId, @GroupId, @GroupName, @GroupTypeId, @GroupTypeName, @SEAP01, @HomePhone, @HomePhonePrincipalInd, @HomePhoneUnlistedInd, @MobilePhone, @MobilePhonePrincipalInd, @MobilePhoneUnlistedInd, @HomeEmail, @HomeEmailPrincipalInd, @WorkEmail, @WorkEmailPrincipalInd, @OtherEmail, @OtherEmailPrincipalInd);", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@IndvId", row[0]);
cmd.Parameters.AddWithValue("@LastName", row[1]);
cmd.Parameters.AddWithValue("@FirstName", row[2]);
cmd.Parameters.AddWithValue("@MiddleName", row[3]);
cmd.Parameters.AddWithValue("@BirthDate", row[4]);
cmd.Parameters.AddWithValue("@IndvAddrLine1", row[5]);
cmd.Parameters.AddWithValue("@IndvAddrLine2", row[6]);
cmd.Parameters.AddWithValue("@IndvAddrLine3", row[7]);
cmd.Parameters.AddWithValue("@IndvCity", row[8]);
cmd.Parameters.AddWithValue("@IndvState", row[9]);
cmd.Parameters.AddWithValue("@IndvZip", row[10]);
cmd.Parameters.AddWithValue("@IndvCountry", row[11]);
cmd.Parameters.AddWithValue("@LocalId", row[12]);
cmd.Parameters.AddWithValue("@LocalName", row[13]);
cmd.Parameters.AddWithValue("@LocalUserId", row[14]);
cmd.Parameters.AddWithValue("@UniservId", row[15]);
cmd.Parameters.AddWithValue("@UniservName", row[16]);
cmd.Parameters.AddWithValue("@UniservUserId", row[17]);
cmd.Parameters.AddWithValue("@ChapterName", row[18]);
cmd.Parameters.AddWithValue("@ChapterUserId", row[19]);
cmd.Parameters.AddWithValue("@EmployerId", row[20]);
cmd.Parameters.AddWithValue("@EmployerName", row[21]);
cmd.Parameters.AddWithValue("@WorkLocationId", row[22]);
cmd.Parameters.AddWithValue("@WorkLocationName", row[23]);
cmd.Parameters.AddWithValue("@WorkLocationUserId", row[24]);
cmd.Parameters.AddWithValue("@GroupId", row[25]);
cmd.Parameters.AddWithValue("@GroupName", row[26]);
cmd.Parameters.AddWithValue("@GroupTypeId", row[27]);
cmd.Parameters.AddWithValue("@GroupTypeName", row[28]);
cmd.Parameters.AddWithValue("@SEAP01", row[29]);
cmd.Parameters.AddWithValue("@HomePhone", row[30]);
cmd.Parameters.AddWithValue("@HomePhonePrincipalInd", row[31]);
cmd.Parameters.AddWithValue("@HomePhoneUnlistedInd", row[32]);
cmd.Parameters.AddWithValue("@MobilePhone", row[33]);
cmd.Parameters.AddWithValue("@MobilePhonePrincipalInd", row[34]);
cmd.Parameters.AddWithValue("@MobilePhoneUnlistedInd", row[35]);
cmd.Parameters.AddWithValue("@HomeEmail", row[36]);
cmd.Parameters.AddWithValue("@HomeEmailPrincipalInd", row[37]);
cmd.Parameters.AddWithValue("@WorkEmail", row[38]);
cmd.Parameters.AddWithValue("@WorkEmailPrincipalInd", row[39]);
cmd.Parameters.AddWithValue("@OtherEmail", row[40]);
cmd.Parameters.AddWithValue("@OtherEmailPrincipalInd", row[41]);
cmd.ExecuteNonQuery();
}
}
}
}
// Copying over the new_users table (with structure and indexes) to the users table.
using (MySqlCommand cmd = new MySqlCommand("DROP TABLE test_users; CREATE TABLE test_users LIKE test_new_users; INSERT test_users SELECT * FROM test_new_users;", con))
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
con.Close();
}
client.Disconnect();
}
else
{
Console.WriteLine("Client cannot be reached...");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
这一切都在“工作”......除了我在 Visual Studio 中处于调试模式时出现错误。关于处理时间太长的事情。哦,你知道吗,CSV 文件中有大约 38,000 行需要插入到表中。
如果您有任何建议或新代码供我试用,我将不胜感激!谢谢!
【问题讨论】:
-
对于导入部分,您应该能够创建一次插入的命令对象,一次添加它的参数,并在循环之前准备它;然后,只需设置参数值并为每次迭代执行。至于第一个“备份”部分;你确定它有效吗?上次我使用 MySqlCommands 时,它们的默认模式不允许在一次执行中进行多个查询。
-
如果慢在解析部分,可以使用ExcelDataReader解析文件,速度很快。如果这是您的问题,我可以分享一个示例代码。
-
速度慢在 INSERTing 部分。在弹出关于它需要很长时间的错误之前,它通过了大约 2,000 行。我一次只解析 1 行并将该行插入到 MySql 表中。我愿意接受有关如何做得更好的建议。 Uueerdo,你能详细介绍一下你评论的前半部分吗?是的,我确信备份部分在单个执行命令中使用多个查询正常工作。
标签: c# mysql visual-studio sql-insert