从文本文件将新行插入数据库

时间:2015-02-19 16:15:38

标签: c# mysql

首先让我说这是我的第一个真正的c#应用程序,所以我知道可能有更有效的方法,但它是我的第一次尝试。无论如何,应用程序的目的是从逗号分隔文件中读取数据,分离该逗号分隔文件的每个元素,然后将这些元素导入数据库中的行。我目前卡在我的try和catch短语中,每次都进入catch,因此它永远不会连接到我的数据库。任何和所有提示,提示或帮助将不胜感激。

当前错误:未处理的类型' System.InvalidOperationException'发生在System.Data.dll

附加信息:连接必须有效并且打开

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 MySql.Data.MySqlClient;
using System.IO;
using System.Data.SqlClient; 
namespace Form1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (File.Exists(@"C:\Users\E15913\Downloads\testfile3.txt"))
                File.Delete(@"C:\Users\E15913\Downloads\testfile3.txt");
            var reader = new StreamReader(File.OpenRead(@"C:\Users\E15913\Downloads\ALMGrade.csv"));
            using (StreamReader readFile = new StreamReader(@"C:\Users\E15913\Downloads\ALMGrade.csv"))
            {
                string newline = "\n";
                string line;
                string[] row;
                int i = 0;

                while ((line = readFile.ReadLine()) != null)
                {

                    i++;
                    row = line.Split(',');
                    System.IO.File.AppendAllText(@"C:\Users\E15913\Downloads\testfile3.txt", row[0].ToString() + Environment.NewLine + row[1].ToString() + Environment.NewLine + row[2].ToString() + Environment.NewLine + row[3].ToString() + Environment.NewLine + row[4].ToString() + Environment.NewLine + row[5].ToString() + Environment.NewLine + row[6].ToString() + Environment.NewLine + row[7].ToString() + Environment.NewLine);
                }

                    string MyConString = "SERVER=10.100.135.220;" + "DATABASE=ak_steel;" + "UID=pickler;" + "PASSWORD=csmrecession09;";

                        string _recDateTimeMySql = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        using (MySqlConnection connection = new MySqlConnection(MyConString))
                        {
                            MySqlCommand cmd = new MySqlCommand("insert into p4gradetable_copy (Grade, CoilingTemp, GaugeRange, WidthRange, MeshCassette1, MeshCassette2, XBow, Elongation ) VALUES(" + '"' + line + '"' + "," + '"' + line + '"' + "," + '"' + line + '"' + "," + '"' + line + '"' + "," + '"' + line + '"' + "," + '"' + line + '"' + "," + '"' + line + '"');
                            cmd.CommandType = CommandType.Text;

                            cmd.Parameters.AddWithValue("@Grade", line);
                            cmd.Parameters.AddWithValue("@CoilingTemp", line);
                            cmd.Parameters.AddWithValue("@GaugeRange", line);
                            cmd.Parameters.AddWithValue("@WidthRange", line);
                            cmd.Parameters.AddWithValue("@MeshCassette1", line);
                            cmd.Parameters.AddWithValue("@MeshCassette2", line);
                            cmd.Parameters.AddWithValue("@XBow", line);
                            cmd.Parameters.AddWithValue("@Elongation", line);

                            connection.Open();
                            cmd.ExecuteNonQuery();
                            label1.Text = "It Works";
                        }




            }
        }
}
}

1 个答案:

答案 0 :(得分:0)

您的问题是您正在创建用于Sql Server的SqlConnection。您必须创建MySqlConnection并确保将MySql客户端DLL链接到您的项目。

还要确保创建MySqlCommand而不是SqlCommand。

区别在于Sql ..类用于SqlServer数据库。 MySql类用于MySql数据库。

绑定参数:

var cmd = new     MySqlCommand("insert into p4gradetable_copy (Grade, CoilingTemp ) VALUES(@Grade, @CoilingTemp)";

仅指定2列用于说明目的。

相关问题