Sun_f0e 2022-01-09 10:16 采纳率: 100%
浏览 53
已结题

C#我做一个,打开指定类型文件并把文件输入到Mysql中,请问我这代码哪里有问题,一直无法输进Mysql中

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://img-mid.csdnimg.cn/release/static/image/mid/ask/756144496146191.png "#left")

  • 写回答

3条回答 默认 最新

查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 1月17日
  • 已采纳回答 1月9日
  • 创建了问题 1月9日

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用