我尝试导入一个excel文件,并在其中一个单元格中包含以下手链字符“(”
如果我不删除此字符,则会导致以下错误。如果我删除该字符没有错误。如何解决这个问题?
[InvalidOperationException:字符串或二进制数据将被截断。] [InvalidOperationException:来自的String类型的给定值 数据源无法转换为指定的类型nvarchar 目标栏。]
以下是代码:
private void UploadData(string path, string dbTableName)
{
//Create connection string to Excel work book
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";
//Create Connection to Excel work book
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
//Create OleDbCommand to fetch data from Excel
excelConnection.Open();
DataTable dbSchema = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); //Get First Sheet Name
OleDbCommand cmd = new OleDbCommand("Select * from [" + dbSchema.Rows[0]["TABLE_NAME"].ToString() + "]", excelConnection);
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString);
//Give your Destination table name
sqlBulk.DestinationTableName = dbTableName;
try
{
sqlBulk.WriteToServer(dReader);
if (dbTableName == "TempTP")
{
SDatabaseManagerData.DatabaseManagerData.UpdateTP();
}
else if (dbTableName == "TempGTIP")
{
SDatabaseManagerData.DatabaseManagerData.UpdateGTIP();
}
lbl_Error.Visible = true;
lbl_Error.Text = "Database updated!";
}
catch (SqlException ex)
{
lbl_Error.Visible = true;
lbl_Error.Text = "Database updated edilemedi! Hata: " + ex.Message;
}
excelConnection.Close();
}