0
Csv文件可能具有多語言字符。使用Unicode字符讀取csv文件會拋出異常
下面是我的代碼在DataView
List<BulkUser> _lstBulkUser = new List<BulkUser>();
DataView BlukUserView;
try
{
using (SqlDataSource _SqlDataSource = new SqlDataSource())
{
//_SqlDataSource.ConnectionString = "Dsn=outlook;dbq=D:\\selvam\\projectlist\\lates\\trunk\\SLVconsoleApp\\SLVconsoleApp.Web\\Uploads\\CSVFiles;defaultdir=D:\\selvam\\projectlist\\lates\\trunk\\SLVconsoleApp\\SLVconsoleApp.Web\\Uploads\\CSVFiles;driverid=27;fil=text;maxbuffersize=2048;pagetimeout=5";
_SqlDataSource.ConnectionString = "Dsn=" + ConfigurationManager.AppSettings["OutlookDns"].ToString() + ";dbq=" + ConfigurationManager.AppSettings["OutlookFileFolder"].ToString() + ";defaultdir=" + ConfigurationManager.AppSettings["OutlookFileFolder"].ToString() + ";driverid=27;fil=text;maxbuffersize=2048;pagetimeout=5";
_SqlDataSource.SelectCommand = "SELECT [First Name] As First_Name, [Last Name] AS LAST_NAME, Company, [E-mail Address] AS EMAILID FROM [" + Filename + "]";
_SqlDataSource.ProviderName = "System.Data.Odbc";
BlukUserView = (DataView)_SqlDataSource.Select(DataSourceSelectArguments.Empty);
int i = 1;
_lstBulkUser = (from value in BlukUserView.Table.Select() select new BulkUser { ID = Convert.ToString(i++), FirstName = Convert.ToString(Encoding.UTF8.GetString((byte[])value["First_Name"])), LastName = value["LAST_NAME"].ToString(), EmailId = value["EMAILID"].ToString(), Company = value["Company"].ToString() }).ToList();
if (File.Exists(ConfigurationManager.AppSettings["OutlookFileFolder"].ToString() + "\\" + Filename))
{
File.Delete(ConfigurationManager.AppSettings["OutlookFileFolder"].ToString() + "\\" + Filename);
}
}
}
catch (Exception ex)
{
}
它拋出ünable to cast object of type 'System.String' to type 'System.Byte[]'.
即使我試圖Unicode字符是沒有看過!我如何解決這個問題?
下面是一個CSV文件
更好地發佈觸發此錯誤的輸入條件以快速獲得幫助。附上的 – varocarbas
截圖。請看一看。 –
我對你的照片上顯示的語言一無所知(不知道語言,邏輯上也不知道編碼)。通常,選擇錯誤的編碼(您應該知道應該應用哪種編碼)會引發讀取不同語言時觸發的錯誤(比您程序中的默認編碼系統佔用的錯誤)。在你的特定代碼中,我不確定對字節執行轉換的意義(實際上是在引發錯誤)。 – varocarbas