简体   繁体   English

CSV到SQL转换器

[英]CSV To SQL Converter

I'm currently having some issues with my CSV to SQL Converter. 我的CSV到SQL转换器当前存在一些问题。 With this being my third week of learning C# I'm starting to grasp some stuff but this is going over my head a bit. 这是我学习C#的第三周,我开始掌握一些东西,但这有点让我感到头疼。

What I'm trying to do is have the Top row/Titles taken down split into each individual title and then for the SQL code through that rather than entering it manually like I've done. 我想做的是将“顶部行/标题”下移为每个标题,然后通过该标题查找SQL代码,而不是像我一样手动输入。 Below you can see some of my code that I've built so far. 在下面,您可以看到我到目前为止构建的一些代码。

private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            // your code here 
            string CSVFilePathName = @"C:\\CSV\\reg.csv";
            string[] Lines = File.ReadAllLines(CSVFilePathName);
            string[] Fields;
            //1st row must be column names; force lower case to ensure matching later on.
            // get regs from filename
            // get fieldnames from Lines[0] (first line of file)
            // create a loop for fields array
            string hdr = Lines[0];
            for (int i = 1; i < Lines.Length; i++)
            {
                Fields = Lines[i].Split(new char[] { ',' });
                CSVTextBox.AppendText(Fields[0] + "," + Fields[1] + "," + Fields[2] + "," + Fields[3] + Environment.NewLine);
                // need a for loop for each field
                // for (
                                    SQLTextBox.AppendText("INSERT INTO[dbo].[REGS]([SESTYPE],[REG],[LFL],[SUBVER]) VALUES('" + Fields[3] + "'" + Fields[0] + "'" + Fields[1] + "'" + Fields[2] + ")" + Environment.NewLine);
                //     }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error is " + ex.ToString());
            throw;
        }

    } 

This all runs at the moment, I'm just struggling to get the titles to become part of the code. 目前所有这些都在运行,我只是在努力使标题成为代码的一部分。 Any help would be appreciated. 任何帮助,将不胜感激。

Cheers, 干杯,

First: Remove the try catch. 首先:删除try catch。 If you get an Exception, you should read, understand and clear off. 如果遇到异常,则应阅读,理解并清除。 For your SQLTextBox: I recommend to use the String.Format function. 对于您的SQLTextBox:我建议使用String.Format函数。 This allows you to create strings with different values, but is much, much easier to read. 这使您可以创建具有不同值的字符串,但是更容易阅读。

For the titles: Use your variable hdr This should contain the title. 对于标题:使用变量hdr这应该包含标题。 Then you can split it via string.Split(',') or string.Split(';'), depending on your delimiter 然后,您可以通过string.Split(',')或string.Split(';')对其进行分割,具体取决于定界符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM