我想知道是否有人可以提供帮助。我正在尝试读取 C# 中的 CSV 文件并将其数据导入到我在 SQL 2008 中创建的表中。
出于某种原因,我不断收到以下错误:
“来自数据源的字符串类型的给定值无法转换为指定目标列的浮点类型。”
完整的堆栈跟踪:
System.Data.SqlClient.SqlBulkCopy.ConvertValue(Object value, _SqlMetaData metadata, Boolean isNull, Boolean& isSqlType, Boolean& coercedToDataFeed)
System.Data.SqlClient.SqlBulkCopy.ReadWriteColumnValueAsync(Int32 col)
System.Data.SqlClient.SqlBulkCopy.CopyColumnsAsync(Int32 col, TaskCompletionSource`1 source)
System.Data.SqlClient.SqlBulkCopy.CopyRowsAsync(Int32 rowsSoFar, Int32 totalRows, CancellationToken cts, TaskCompletionSource`1 source)
System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsyncContinued(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsync(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestContinuedAsync(BulkCopySimpleResultSet internalResults, CancellationToken cts, TaskCompletionSource`1 source)
System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestAsync(CancellationToken cts, TaskCompletionSource`1 source)
System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalAsync(CancellationToken ctoken)
System.Data.SqlClient.SqlBulkCopy.WriteRowSourceToServerAsync(Int32 columnCount, CancellationToken ctoken)
System.Data.SqlClient.SqlBulkCopy.WriteToServer(DataTable table, DataRowState rowState)
System.Data.SqlClient.SqlBulkCopy.WriteToServer(DataTable table)
RMBEventReportingSystemPOC.Admin.ProcessFile(String strFilename) in \Admin.aspx.cs:line 159
请在下面找到我正在使用的代码。请让我知道我哪里出错了:
StatusLabel.Text = "File Process status: File process started!";
try
{
//var fileName = string.Format(strFilename, Directory.GetCurrentDirectory());
SqlConnection con = new SqlConnection(@"**ConnectionString**");
string filepath = Server.MapPath("~/uploads/") + strFilename;
StreamReader sr = new StreamReader(filepath);
string line = sr.ReadLine();
string[] value = line.Split(';');
DataTable dt = new DataTable();
DataRow row;
foreach (string dc in value)
{
if (dc == "Month" || dc == "Year" || dc == "Reply")
dt.Columns.Add(new DataColumn(dc, typeof(int)));
else if (dc == "CostPerHead" || dc == "TotalCost")
dt.Columns.Add(new DataColumn(dc, typeof(float)));
else
dt.Columns.Add(new DataColumn(dc));
}
while (!sr.EndOfStream)
{
value = sr.ReadLine().Split(';');
if (value.Length == dt.Columns.Count)
{
row = dt.NewRow();
//fix up default values
value[1] = value[1] == "" ? "0" : value[1].ToString().Trim();
value[2] = value[2] == "" ? "0" : value[2].ToString().Trim();
value[3] = value[3] == "" ? "0.00" : string.Format("{0:0.00}",value[3].ToString());
value[4] = value[4] == "" ? "0.00" : string.Format("{0:0.00}",value[4].ToString());
value[7] = value[7] == "" ? "0" : value[7].ToString().Trim();
row.ItemArray = value;
dt.Rows.Add(row);
}
}
////fix up default values
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i][1] = int.Parse(dt.Rows[i][1].ToString().Trim());
dt.Rows[i][2] = int.Parse(dt.Rows[i][2].ToString().Trim());
dt.Rows[i][3] = Math.Round(float.Parse(dt.Rows[i][3].ToString() + ".00"), 2);
dt.Rows[i][4] = Math.Round(float.Parse(dt.Rows[i][4].ToString() + ".00"), 2);
dt.Rows[i][7] = dt.Rows[i][7].ToString() == "" ? 0 : int.Parse(dt.Rows[i]["Reply"].ToString().Trim());
}
SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
bc.DestinationTableName = "Guestlist";
bc.BatchSize = dt.Rows.Count;
// add column mappings if necessary
con.Open();
bc.WriteToServer(dt);
bc.Close();
con.Close();
StatusLabel.Text = "File Process status: File process completed! " + dt.Rows.Count.ToString() + " records imported.";
StatusLabel.CssClass = "success";
}
catch (Exception exx)
{
StatusLabel.Text = "File Process status: File process failed!: " + exx.Message;
StatusLabel.CssClass = "error";
}
任何指针将不胜感激。