首页 > 解决方案 > 列 id 不属于表

问题描述

我正在尝试将 CSV 文件导入 SQL Server 数据库

这是我的代码,找不到问题:(

foreach(DataRow importRow in KeminRecipe.Rows) {

  SqlCommand cmd = new SqlCommand("INSERT INTO KeminRecipe (ID,DateAndTime,Recipe,ReadingSensor,LabResult,MoistureSensor,DryPoint,WetPoint,Enabled,Error,BatchTime,BatchSize,MoistAdd,ProductAIncluded,ProductBIncluded,LastUser,LastAccess) " + "VALUES (@ID,@DateAndTime,@Recipe,@ReadingSensor,@LabResult,@MoistureSensor,@DryPoint,@WetPoint,@Enabled,@Error,@BatchTime,@BatchSize,@MoistAdd,@ProductAIncluded,@ProductBIncluded,@LastUser,@LastAccess)", conn);

//here the error displays starting with ID//  cmd.Parameters.AddWithValue("@ID", importRow["ID"]);
  cmd.Parameters.AddWithValue("@DateAndTime", importRow["DateAndTime"]);
  cmd.Parameters.AddWithValue("@Recipe", importRow["Recipe"]);
  cmd.Parameters.AddWithValue("@ReadingSensor", importRow["ReadingSensor"]);
  cmd.Parameters.AddWithValue("@LabResult", importRow["LabResult"]);
  cmd.Parameters.AddWithValue("@MoistureSensor", importRow["MoistureSensor"]);
  cmd.Parameters.AddWithValue("@DryPoint", importRow["DryPoint"]);
  cmd.Parameters.AddWithValue("@WetPoint", importRow["WetPoint"]);
  cmd.Parameters.AddWithValue("@Enabled", importRow["Enabled"]);
  cmd.Parameters.AddWithValue("@Error", importRow["Error"]);
  cmd.Parameters.AddWithValue("@BatchTime", importRow["BatchTime"]);
  cmd.Parameters.AddWithValue("@BatchSize", importRow["BatchSize"]);
  cmd.Parameters.AddWithValue("@MoistAdd", importRow["MoistAdd"]);
  cmd.Parameters.AddWithValue("@ProductAIncluded", importRow["ProductAIncluded"]);
  cmd.Parameters.AddWithValue("@ProductBIncluded", importRow["ProductBIncluded"]);
  cmd.Parameters.AddWithValue("@LastUser", importRow["LastUser"]);
  cmd.Parameters.AddWithValue("@LastAccess", importRow["LastAccess"]);

  cmd.ExecuteNonQuery();

}

private void BROWSE(object sender, EventArgs e)
    {

        OpenFileDialog CSV = new OpenFileDialog();
        CSV.DefaultExt = ".csv";
        CSV.Filter = "Comma Separated (*.csv)|*.csv";
        CSV.ShowDialog();

        CsvFolderPath.Text = CSV.FileName;
    }

private void IMPORT(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(CsvFolderPath.Text )) 
        {
            MessageBox.Show("Please choose a csv file");
            return;
        }

        else
        {
            DataTable KeminRecipe = GetDataFromFile();

            if (KeminRecipe != null) ;
            SaveImportDataToDatabase(KeminRecipe);

            MessageBox.Show("Import Complete");
        }



    }

CSV 数据:

“ID”;“日期和时间”;“配方”;“读数传感器”;“实验室结果 %”;“湿度传感器 %”;“干点”;“湿点”;“启用”;“错误”; “批次时间 [s]”;“批次大小 [kg]”;“潮湿。添加。[%]”;“包含产品 1”;“包含产品 2”;“LastUser”;“LastAccess”

19;04/21/2016 09:45:49;101;4448;8.8;0.0;1;0;0;-1.19;309;2000;0.0;1;0;运营商;11/02/2018 00: 00:01

给我一个错误:

列 id 不属于表

标签: c#sql-server

解决方案


最后我找到了解决我的问题的方法。答案由两个解决方案组成。

1)在我的情况下,带有 csv 扩展名的文件用 ; 分隔 而不是,。在我的代码中,我使用 , 分隔流了 csv 文件(streamreader)。这就是它不起作用的原因。VS 将数据变为一列而不是多列。

 string[] headerColumns = header.Split(';'); // so i changed , to ;
                foreach (string headerColumn in headerColumns)
                {
                    CSVData.Columns.Add(headerColumn);
                }

                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();

                    if (string.IsNullOrEmpty(line)) continue;

                    string[] fields = line.Split(';'); // so i changed , to ;
                    DataRow importedRow = CSVData.NewRow();

2) CSV 文件包含作为“列标题”的列。我必须删除“”,这样我的代码才能识别我在我的 sql 数据库中使用的标头。

我发现这两种解决方案都使用 VS 属性来检查我的字符串或数据表中包含哪些数据。


推荐阅读