我正在创建一个数据表:
public DataTable dt;
public ReadFileIntoDataTable(string inputfile)
{
dt = new DataTable();
dt.Columns.Add("Specimen_ID",typeof(string));
dt.Columns.Add("Client_Key",typeof(int));
dt.Columns.Add("Outcome",typeof(string));
dt.Columns.Add("Medications",typeof(string));
dt.Columns.Add("Date_Collected",typeof(DateTime));
dt.Columns.Add("Time_Collected",typeof(DateTime));
dt.Columns.Add("Date_Entered",typeof(DateTime));
dt.Columns.Add("Time_Entered",typeof(DateTime));
dt.Columns.Add("Date_Completed",typeof(DateTime));
dt.Columns.Add("Time_Completed",typeof(DateTime));
dt.Columns.Add("Test_Date",typeof(DateTime));
dt.Columns.Add("Test_Time",typeof(DateTime));
dt.Columns.Add("Practice_Name",typeof(string));
dt.Columns.Add("Practice_Code",typeof(string));
dt.Columns.Add("Client_ID",typeof(string));
dt.Columns.Add("Requesting_Physician",typeof(string));
dt.Columns.Add("Other_Medications",typeof(string));
dt.Columns.Add("Order_Comments",typeof(string));
dt.Columns.Add("Reference_Number",typeof(string));
dt.Columns.Add("Order_Count",typeof(int));
dt.Columns.Add("_6_MAM_HEROIN_METABOLITE",typeof(decimal));
dt.Columns.Add("_6_MAM_N_HEROIN_METABOLITE",typeof(decimal));
dt.Columns.Add("_6_MAM_S_HEROIN_METABOLITE",typeof(decimal));
dt.Columns.Add("_6_MAM_SN_HEROIN_METABOLITE",typeof(decimal));
dt.Columns.Add("_6_MAM_SP_HEROIN_METABOLITE",typeof(decimal));
...
..
..
string Specimen_ID;
string Client_Key;
string Outcome;
string Medications;
string Date_Collected;
string Time_Collected;
string Date_Entered;
string Time_Entered;
string Date_Completed;
string Time_Completed;
string Test_Date;
string Test_Time;
string Practice_Name;
string Practice_Code;
string Client_ID;
string Requesting_Physician;
string Other_Medications;
string Order_Comments;
string Reference_Number;
string Order_Count;
string _6_MAM_HEROIN_METABOLITE;
string _6_MAM_N_HEROIN_METABOLITE;
string _6_MAM_S_HEROIN_METABOLITE;
string _6_MAM_SN_HEROIN_METABOLITE;
string _6_MAM_SP_HEROIN_METABOLITE;
string _6ACE_C_6_MAM__Heroin_metabolite;
.....
........
.....
using (GenericParser parser = new GenericParser())
{
parser.SetDataSource(inputfile);
char[] delimiters = new char[] { ',' };
parser.ColumnDelimiter = delimiters[0];
parser.FirstRowHasHeader = true;
//parser.SkipDataRows = 10;
parser.MaxBufferSize = 4096;
parser.MaxRows = 500;
parser.TextQualifier = '\"';
while (parser.Read())
{
Specimen_ID = parser["Specimen ID"];
Client_Key = parser["Client Key"];
Outcome = parser["Outcome"];
Medications = parser["Medications"];
Date_Collected = parser["Date Collected"];
Time_Collected = parser["Time Collected"];
Date_Entered = parser["Date Entered"];
Time_Entered = parser["Time Entered"];
Date_Completed = parser["Date Completed"];
Time_Completed = parser["Time Completed"];
Test_Date = parser["Test Date"];
Test_Time = parser["Test Time"];
Practice_Name = parser["Practice Name"];
Practice_Code = parser["Practice Code"];
Client_ID = parser["Client ID"];
Requesting_Physician = parser["Requesting Physician"];
Other_Medications = parser["Other Medications"];
Order_Comments = parser["Order Comments"];
Reference_Number = parser["Reference Number"];
Order_Count = parser["Order Count"];
_6_MAM_HEROIN_METABOLITE = parser["6-MAM-HEROIN METABOLITE"];
_6_MAM_N_HEROIN_METABOLITE = parser["6-MAM_N-HEROIN METABOLITE"];
_6_MAM_S_HEROIN_METABOLITE = parser["6-MAM_S-HEROIN METABOLITE"];
_6_MAM_SN_HEROIN_METABOLITE = parser["6-MAM_SN-HEROIN METABOLITE"];
_6_MAM_SP_HEROIN_METABOLITE = parser["6-MAM_SP-HEROIN METABOLITE"];
_6ACE_C_6_MAM__Heroin_metabolite = parser["6ACE_C-6-MAM (Heroin metabolite)"];
.....
......
.......
DataRow newRow = dt.NewRow();
newRow["Specimen_ID"] = Specimen_ID;
newRow["Client_Key"] = Client_Key;
newRow["Outcome"] = Outcome;
newRow["Medications"] = Medications;
newRow["Date_Collected"] = Date_Collected;
newRow["Time_Collected"] = Time_Collected;
newRow["Date_Entered"] = Date_Entered;
newRow["Time_Entered"] = Time_Entered;
newRow["Date_Completed"] = Date_Completed;
newRow["Time_Completed"] = Time_Completed;
newRow["Test_Date"] = Test_Date;
newRow["Test_Time"] = Test_Time;
newRow["Practice_Name"] = Practice_Name;
newRow["Practice_Code"] = Practice_Code;
newRow["Client_ID"] = Client_ID;
newRow["Requesting_Physician"] = Requesting_Physician;
newRow["Other_Medications"] = Other_Medications;
newRow["Order_Comments"] = Order_Comments;
newRow["Reference_Number"] = Reference_Number;
newRow["Order_Count"] = Order_Count;
newRow["_6_MAM_HEROIN_METABOLITE"] = ????
newRow["_6_MAM_N_HEROIN_METABOLITE"] = _6_MAM_N_HEROIN_METABOLITE;
newRow["_6_MAM_S_HEROIN_METABOLITE"] = _6_MAM_S_HEROIN_METABOLITE;
newRow["_6_MAM_SN_HEROIN_METABOLITE"] = _6_MAM_SN_HEROIN_METABOLITE;
newRow["_6_MAM_SP_HEROIN_METABOLITE"] = _6_MAM_SP_HEROIN_METABOLITE;
.........
........
......
如您所见,我正在执行以下操作:
- 创建数据表
- 使用 genericparser 解析文件
- 从文件中插入数据到数据表
我的问题是文件没有 DECIMAL 类型字段的值。我收到以下错误:
System.ArgumentException: Input string was not in a correct format.Couldn't store <> in _6_MAM_HEROIN_METABOLITE Column. Expected type is Decimal. ---> System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.String.System.IConvertible.ToDecimal(IFormatProvider provider)
at System.Data.Common.DecimalStorage.Set(Int32 record, Object value)
at System.Data.DataColumn.set_Item(Int32 record, Object value)
--- End of inner exception stack trace ---
at System.Data.DataColumn.set_Item(Int32 record, Object value)
at System.Data.DataRow.set_Item(DataColumn column, Object value)
at CreateTableScript.ReadFileIntoDataTable..ctor(String inputfile) in C:\Users\agordon\Documents\Visual Studio 2008\Projects\SubversionRepository\Internal Application\SalesDWH\CreateTableScript\CreateTableScript\ReadFileIntoDataTable.cs:line 930
at CreateTableScript.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\agordon\Documents\Visual Studio 2008\Projects\SubversionRepository\Internal Application\SalesDWH\CreateTableScript\CreateTableScript\Form1.cs:line 33
然后根本不设置该值,将使用数据表中分配的默认值。您可以将默认值更改为 0、非零或 DbNull.Value
我是一名优秀的程序员,十分优秀!