Sun_f0e 2022-01-09 02: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月16日
  • 已采纳回答 1月9日
  • 创建了问题 1月9日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部