- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这就是我想要实现的目标......点击按钮,我想要:
我不是一名日常程序员,并且已经很长时间没有创建程序了。但我将一些代码拼凑在一起以获得一些可以工作的东西。以下是我当前的代码:
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 中处于 Debug模式时出现错误。关于处理时间过长的问题。哦,正如您所知,CSV 文件中有大约 38,000 行必须插入到表中。
如果您有任何建议或新代码供我尝试,我将不胜感激!谢谢!
最佳答案
在许多情况下,您最终会循环访问数据,对不同的数据重复运行相同的查询,您可以通过在循环开始之前准备一次查询来帮助加快速度。
using (var cn = new MySqlConnection(...))
using (var cmd = cn.CreateCommand())
{
// Setup command
cmd.CommandText = "Some query";
var param1 = cmd.Parameters.Add("@paramName1", MySQLDbType.sometype);
var param2 = cmd.Parameters.Add("@paramName2", MySQLDbType.sometype);
cmd.Prepare();
// get data
// loop over data
foreach(var d in data)
{
param1.Value = d.somevalue;
param2.Value = d.anothervalue;
cmd.ExecuteNonQuery();
}
}
您还可以通过索引来寻址参数,例如 cmd.Parameters[0].Value
。
以这种方式准备语句可以减少每次迭代解析查询本身的工作开销。
<小时/>在我必须处理的一种情况下,我发现最好的解决方案是将上述内容与使用多个值列表的 INSERT 查询相结合,其形式为 INSERT INTO X(a,b,c) VALUES ( ?,?,?),(?,?,?),(?,?,?)
。它使实际的实现变得更加复杂(必须适当处理最后不会填充所有值列表的“剩余部分”),并且依赖于按索引寻址参数;但确实有所作为。
编辑:处理 ssh 连接关闭的伪代码
connect = true
Open datasource
while (data remains)
{
if (connect)
{
init ssh client
init mysql connection
init mysql command
connect = false
}
while (data remains and enough time remaining)
{
get next data
set command parameters
execute command
}
if (not enough time remaining)
{
close current connection and client
connect = true
}
}
关于c# - 需要优化 C# 代码以将 CSV 行插入 MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57825103/
我有两个 csv 文件 file1.csv 和 file2.csv。 file1.csv 包含 4 列。 文件1: Header1,Header2,Header3,Header4 aaaaaaa,bb
我想知道是否有任何方法可以在导入数据库之前测试 CSV 文件? 我有一个包含多列的巨大 CSV 文件,每列都有不同的数据类型和大小。如何测试生成的 CSV 文件中出现的数据是否与每列的大小一致? 还有
我正在从 SCOM 中提取服务器列表,并希望根据包含以下数据的 CSV 检查此列表: Computername,Collection Name Server01,NA - All DA Servers
我有一个包含 24 列的 csv 文件。其中我只想阅读 3 列。我看到 super CSV 是一个非常强大的库,但我不知道如何部分读取 CSV。 partial reading上的链接坏了。 请帮我举
我正在尝试将加特林日志文件导出到 CSV,因为我需要更新 google 电子表格中的所有全局值,因为我的经理需要电子表格中的值。 最佳答案 此 CSV 文件被删除并替换为 JSON 文件,名为 glo
我对 csv 是结构化数据还是半结构化数据感到困惑。 就像 RDBMS 是一个有关系的结构化数据,但 csv 没有关系。 我无法找到确切的答案。 最佳答案 我可以说,具有恒定列和行(二维)的 CSV
我正在使用 pipes-csv 库读取一个 csv 文件。我想先读第一行,然后再读其余的。不幸的是,在 Pipes.Prelude.head 函数返回之后。管道正在以某种方式关闭。有没有办法先读取 c
起初这似乎很明显,但现在我不太确定。 如果 CSV 文件具有以下行: a, 我会将其解释为具有值“a”和“”的两个字段。但是然后查看一个空行,我可以很容易地争辩说它表示一个值为“”的字段。 我接受文件
我正在尝试将列表字典写入 CSV 文件。我希望这些键是 CSV 文件的标题,以及与该键关联的列中的每个键关联的值。 如果我的字典是: {'600': [321.4, 123.5, 564.1, 764
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
是否有任何官方方法允许 CSV 格式的文件允许评论,无论是在其自己的行上还是在行尾? 我尝试检查wikipedia关于此以及RFC 4180但两者都没有提到任何让我相信它不是文件格式的一部分的内容,所
我有一些 csv 格式的数据。然而它们已经是一个字符串,因为我是从 HTTP 请求中获取它们的。我想使用数据框来查看数据。但是我不知道如何解析它,因为 CSV 包只接受文件,而不接受字符串。 一种解决
我有一个 CSV 文件,其中包含一些字段的值列表。它们作为 HTML“ul”元素存储在数据库中,但我想将它们转换为对电子表格更友好的东西。 我应该使用什么作为分隔符?我可以使用转义的逗号、竖线、分号或
我使用 Google 表格(电子表格)来合并我的 Gambio 商店的不同来源的文章数据。要导入数据,我需要在 .csv 文件中使用管道符号作为分隔符/分隔符,并使用 "作为文本分隔符。在用于导出为
这是一个奇怪的请求,因为我们都知道数据库头不应该包含空格。 但是,我正在使用的系统需要在其标题中使用空格才能导入。 我创建了一个 Report Builder 报告,它将数据构建到一个表中,并在我运行
我有一个 .csv 文件,我需要将其转换为 coldfusion 查询。我使用了 cflib.org CSVtoQuery 方法,它工作正常......但是...... 如果 csv 中的“单元格”在
我想知道是否有任何方法可以生成文化中性 CSV 文件,或者至少指定文件中存在的特定列的数据格式。 例如,我生成了包含带小数点分隔符 (.) 的数字的 CSV 文件,然后 将其传递给小数点分隔符为 (,
我正在构建一个 CSV 字符串 - 因此用户单击 div 的所有内容 - 5 个字符的字符串都会传递到隐藏字段中 - 我想做的是附加每个新值并创建一个 CSV 字符串 - 完成后- 在文本框中显示 -
我正在努力从另外两个文件创建一个 CSV 文件 这是我需要的 我想要的文件(很多其他行) “AB”;“A”;“B”;“C”;“D”;“E” 我拥有的文件: 文件 1:"A";"B";"C";"D";"
我正在尝试将表导出到配置单元中的本地 csv 文件。 INSERT OVERWRITE LOCAL DIRECTORY '/home/sofia/temp.csv' ROW FORMAT DELIMI
我是一名优秀的程序员,十分优秀!