基于CSV文件中的列创建表

本文关键字:创建 CSV 文件 基于 | 更新日期: 2023-09-27 18:07:53

我有一个基于文件(DBF, Excel)的列创建一个新表的现有代码:

OleDbConnection oConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + tbXLSBrowse.Text + "';Extended Properties='"Excel 12.0 xml;HDR=Yes;IMEX=1'"");
OleDbCommand command = new OleDbCommand("Select * FROM [Sheet1$]", oConn); //change to the sheet name
oConn.Open();
DataTable dt = new DataTable();
dt.Load(command.ExecuteReader());
oConn.Close();
DataTableReader reader = dt.CreateDataReader();
myConnection = new SqlConnection(cString);
myConnection.Open();
// checking whether the table selected from the dataset exists in the database or not
string exists = null;
try
{
    SqlCommand cmd = new SqlCommand("SELECT * FROM sysobjects where name = '" + tb.Text + "'", myConnection);
    exists = cmd.ExecuteScalar().ToString();
    //MessageBox.Show("EXISTS");
}
catch (Exception exce)
{
    exists = null;
    //MessageBox.Show("DOESNT EXIST");
}
if (exists == null)
{
    // selecting each column of the datatable to create a table in the database
    foreach (DataColumn dc in dt.Columns)
    {
        if (exists == null)
        {
            SqlCommand createtable = new SqlCommand("CREATE TABLE " + tb.Text + " (" + dc.ColumnName + " varchar(MAX))", myConnection);
            createtable.ExecuteNonQuery();
            exists = tbXLSTableName.Text;
        }
        else
        {
            SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + tb.Text + " ADD [" + dc.ColumnName + "] varchar(MAX)", myConnection);
            addcolumn.ExecuteNonQuery();
        }
    }
}

文本框如下:

//tbXLSBrowse.Text = the excel file name;
//tb.Text = user generated table name;

以上代码是Excel文件的示例。我有一个CSV,我也试图做同样的,但不确定如何做到这一点。

我有以下代码,读取CSV文件中的每行,获取每行的字段,并将其添加到List数组:

var lines = File.ReadLines(textBox1.Text);
List<string> colArray = new List<string>();
foreach (string line in lines) //for each line
{
    using (TextFieldParser parser = new TextFieldParser(textBox1.Text))
    {
        parser.TextFieldType = FieldType.Delimited;
        parser.SetDelimiters(",");
        while (!parser.EndOfData) //while file is being read
        {
            string[] fields = parser.ReadFields();
            foreach (string field in fields) //for each column
            {
                colArray.Add(field);
                colArray.ToArray();
            }
            MessageBox.Show(colArray.Count + ""); //displays the count for the columns for each line
            colArray.Clear(); //clear the column to use it for next line
        }
    }
}

如何将开头的代码与上面的代码结合起来执行以下操作:

  • 读取CSV文件
  • 根据第一行计数(这是文件的头)创建一个SQL表

还是不可能?我想这样做的原因是因为每行有329列,如果我能够用代码完成这一点,从长远来看,它将节省很多时间。

这个网站会有任何帮助:CSV到SQL

基于CSV文件中的列创建表

您不需要在第二个代码片段中展开所有列名。一旦读取了文件,就可以在字符串上使用split命令将头行拆分为列表(只需使用索引访问文件的第一行)。然后,一旦有了列名列表,就可以使用foreach循环遍历它们,并以与在上述代码中相同的方式创建表。只需替换字符串项的data列。

var lines = File.ReadLines(textBox1.Text);
List<string> headerRow = lines.ElementAt(0).Split(',').ToList();
foreach (string header in headerRow)
{
   Create table etc.......
}

我希望这有助于,代码可能不是完全正确,但应该给你一个想法,你需要什么