using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _1y8h
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static string connString = "server=127.0.0.1;port=3306;user=root;password=123456;database=fy8h;";
public static MySqlConnection connection = new MySqlConnection(connString);
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "文档(*.csv;*.csv|*.csv;*.csv";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (openFileDialog1.FileName != "")
{
txtFile.Text = openFileDialog1.FileName;
}
MessageBox.Show("添加文件成功");
}
}
public void Read(string Path)
{
StreamReader sr = new StreamReader(Path, Encoding.UTF8);
string line;
while ((line = sr.ReadLine()) != null)
{
int i = 0;
while (i < 1)
{
if (i == 0)
{
i++;
continue;
}
}
Console.WriteLine(line.ToString());
string[] strArr = line.Split(',');
for (int ii = 0; ii < strArr.Length; ii++)
{
strArr[ii] = strArr[ii].Replace('"', ' ').Trim();
}
if (connection.State == ConnectionState.Closed)
connection.Open();
string sql = $"insert into t_plcdata (F_ID,F_Address,F_DataType,F_Len,F_Valid,F_Memo,F_Function," +
$"F_Form,F_DecimalNum,F_Multiple,f_alertflag,F_Index," +
$"F_EventFlag)value ('{strArr[0]}','{strArr[1]}','{strArr[2]}" +
$"','{strArr[3]}','{strArr[4]}','{strArr[5]}','{strArr[6]}'," +
$"'{strArr[7]}','{strArr[8]}','{strArr[9]}','{strArr[10]}','{strArr[11]}','{strArr[12]}')";
}
}
private void btnSql_Click(object sender, EventArgs e)
{
Read(txtFile.Text);
}
![img](https: