这是我的数据表:
DataTable CSVFile = new DataTable();
CSVFile.Columns.Add("Occurrence_Date", typeof(DateTime));
CSVFile.Columns.Add("Preanalytical_Before_Testing", typeof(string));
CSVFile.Columns.Add("Cup_Type", typeof(string));
CSVFile.Columns.Add("Analytical_Testing_Phase", typeof(string));
CSVFile.Columns.Add("Area", typeof(string));
CSVFile.Columns.Add("Postanalytical_After_Testing", typeof(string));
CSVFile.Columns.Add("Other", typeof(string));
CSVFile.Columns.Add("Practice_Code", typeof(string));
CSVFile.Columns.Add("Comments", typeof(string));
我正在尝试添加一个新行:
DataRow newRow = CSVFile.NewRow();
newRow["Occurrence_Date"] = Convert.ToDateTime(splitcsvlines[GetColumnsNames.Occurrence_Date]);
newRow["Preanalytical_Before_Testing"] = splitcsvlines[GetColumnsNames.Preanalytical_Before_Testing];
newRow["Cup_Type"] = splitcsvlines[GetColumnsNames.Cup_Type];
newRow["Analytical_Testing_Phase"] = splitcsvlines[GetColumnsNames.Analytical_Testing_Phase];
newRow["Area"] = splitcsvlines[GetColumnsNames.Area];
newRow["Postanalytical_After_Testing"] = splitcsvlines[GetColumnsNames.Postanalytical_After_Testing];
newRow["Other"] = splitcsvlines[GetColumnsNames.Other];
newRow["Practice_Code"] = splitcsvlines[GetColumnsNames.Practice_Code];
newRow["Comments"] = splitcsvlines[GetColumnsNames.Comments];
CSVFile.Rows.Add(newRow);
但是我收到错误
'BulkUploadToLOMDatabase.GetColumnsNames'的类型初始值设定项引发异常。
我尝试过newRow["Cup_Type"] = "something"
或newRow[2] = "something"
但仍然遇到同样的错误
以下是该课程的内容:
class GetColumnsNames
{
public static int Occurrence_Date = Convert.ToInt16(ConfigurationSettings.AppSettings["Occurrence_Date"].ToString());
public static int Preanalytical_Before_Testing = Convert.ToInt16(ConfigurationSettings.AppSettings["1_0_Preanalytical_Before_Testing"].ToString());
public static int Cup_Type = Convert.ToInt16(ConfigurationSettings.AppSettings["Cup_Type"].ToString());
public static int Analytical_Testing_Phase = Convert.ToInt16(ConfigurationSettings.AppSettings["Analytical_Testing_Phase"].ToString());
public static int Area = Convert.ToInt16(ConfigurationSettings.AppSettings["Area"].ToString());
public static int Postanalytical_After_Testing = Convert.ToInt16(ConfigurationSettings.AppSettings["Postanalytical_After_Testing"].ToString());
public static int Other = Convert.ToInt16(ConfigurationSettings.AppSettings["Other"].ToString());
public static int Practice_Code = Convert.ToInt16(ConfigurationSettings.AppSettings["Practice_Code"].ToString());
public static int Comments = Convert.ToInt16(ConfigurationSettings.AppSettings["Comments"].ToString());
}
我做错了什么?我班上有什么问题?
答案 0 :(得分:2)
The type initializer for BulkUploadToLOMDatabase.GetColumnsNames' threw an exception.
这意味着CLR尝试创建您的类型但失败了。由于您有许多依赖于KeyNotFound
FormatExecption
NullException
失败的调用的静态变量,您应该将此类转换为singleton。
这样您就可以正确捕获ConfigurationSettings.AppSettings
的任何问题另外,您可能需要考虑使用system.configuration.configurationmanager。这是除非你在1.1。 ConfigurationSettings.AppSettings在2.0中已经过时了
答案 1 :(得分:1)
确保ConfigurationSettings.AppSettings具有您要求的那些整数值。看起来这就是抛出错误。
您的数据表和数据路码很好。
答案 2 :(得分:1)
我认为问题出现是因为这行代码:
public static int Occurrence_Date = Convert.ToInt16(ConfigurationSettings.AppSettings["Occurrence_Date"].ToString());
int32值无法转换为DateTime值:
newRow["Occurrence_Date"] = Convert.ToDateTime(splitcsvlines[GetColumnsNames.Occurrence_Date]);
正如@LarsTech所说,您的dataTable相关代码是正确的。最危险的是我上面发布的代码。请在一个简单的控制台中实现它(ConfigurationSettings.AppSettings [“Occurrence_Date”]。ToString())特定值转换为int15然后转换为DateTime。你有什么结果?
答案 3 :(得分:1)
正如其他人所提到的,构建/加载表格很好......这里是快速测试,使用序号位置与列名称来加载10个随机值。您在评论中说调试显示正确的值,如果是,那么一切都应该正常工作。只需“确保”您获得每个列值的预期数据类型。
DataTable CSVFile = new DataTable();
CSVFile.Columns.Add("Occurrence_Date", typeof(DateTime));
CSVFile.Columns.Add("Preanalytical_Before_Testing", typeof(string));
CSVFile.Columns.Add("Cup_Type", typeof(string));
CSVFile.Columns.Add("Analytical_Testing_Phase", typeof(string));
CSVFile.Columns.Add("Area", typeof(string));
CSVFile.Columns.Add("Postanalytical_After_Testing", typeof(string));
CSVFile.Columns.Add("Other", typeof(string));
CSVFile.Columns.Add("Practice_Code", typeof(string));
CSVFile.Columns.Add("Comments", typeof(string));
for (int i = 0; i < 10; i++)
{
DataRow row = CSVFile.NewRow();
row[0] = DateTime.Now.AddDays(i);
row[1] = "Field 2";
row[2] = "Field 3";
row[3] = "Field 4";
row[4] = "Field 5";
row[5] = "Field 6";
row[6] = "Field 7";
row[7] = "Field 8";
row[8] = "Field 9";
CSVFile.Rows.Add(row);
}