c# - 列 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 不属于表
解决方案
最后我找到了解决我的问题的方法。答案由两个解决方案组成。
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 属性来检查我的字符串或数据表中包含哪些数据。
推荐阅读
- python - 压缩numpy浮点数组
- authentication - linux脚本中LDAP的用户认证测试
- python - 如何在 Python(Zeep 模块)中修复“'str'对象没有属性'keys'”?
- python - 用值更新字符串的最后一个字符
- powershell - Powershell GzipStream,“找不到“GZipStream”的重载和参数计数:“2”
- nuxt.js - 如何始终重定向到 nuxtjs 中的浏览器语言页面?
- php - android 和 php api 的响应是正确的,而通过 android 应用程序删除数据时没有删除数据?
- javascript - 如何在博客页面中获取搜索栏以搜索精选文章?
- php - 已解决:当 MySQL 数据包含重音符号时,PHP json_encode 返回 BLANK
- javascript - 使用 vue-virtual-scroler 和 Vuetify 2.0 滚动时出现视觉故障 - 不流畅的可视化