.csvファイルをデータベースにインポートしようとしています。私はデータベースにExcelワークシートをインポートすることができますが、.xlsからの.csvとは異なるファイル形式のため、.csv用のインポート関数を特別に作成する必要があります。ExcelConnectionを開く際にエラーが発生しました。C#VS2005
以下は私のコードです:
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
// Get the name of the Excel spreadsheet to upload.
string strFileName = Server.HtmlEncode(FileUpload1.FileName);
// Get the extension of the Excel spreadsheet.
string strExtension = Path.GetExtension(strFileName);
// Validate the file extension.
if (strExtension != ".xls" && strExtension != ".xlsx" && strExtension != ".csv" && strExtension != ".csv")
{
Response.Write("<script>alert('Failed to import DEM Conflicting Role Datasheet. Cause: Invalid Excel file.');</script>");
return;
}
// Generate the file name to save.
string strUploadFileName = @"C:\Documents and Settings\rhlim\My Documents\Visual Studio 2005\WebSites\SoD\UploadFiles\" + DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;
// Save the Excel spreadsheet on server.
FileUpload1.SaveAs(strUploadFileName);
// Create Connection to Excel Workbook
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strUploadFileName + ";Extended Properties=Text;";
using (OleDbConnection ExcelConnection = new OleDbConnection(connStr)){
OleDbCommand ExcelCommand = new OleDbCommand("SELECT [columns] FROM +userrolelist", ExcelConnection);
OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
ExcelConnection.Open();
using (DbDataReader dr = ExcelCommand.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=<IP>;Initial Catalog=<DB>;User ID=<userid>;Password=<password>";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "DEMUserRoles";
bulkCopy.WriteToServer(dr);
Response.Write("<script>alert('DEM User Data imported');</script>");
}
}
}
}
else Response.Write("<script>alert('Failed to import DEM User Roles Data. Cause: No file found.');</script>");
}
ファイルが正常に保存されていますが、エラーは、ファイルが正常に保存されているにもかかわらず、ファイルのパスが有効でないことを言いますas .csvとして、私はデータベースにデータをインポートするプロセスを続行できません。 csvファイルが正常に保存されているが、私はcsvファイルが保存されたファイルのパスが有効でないというエラーが生じています結論として
:
以下は私のエラーのスクリーンショットです。経験豊富な人から助けが必要です。ありがとうございます
ありがとうございましたcampbell、それは私のために働いているようです。しかし、パイプで区切られたテキストファイルを.csvファイルに変換するときや、いくつかの列変数を2つの部分に分割して次の列にスキップして行をめちゃくちゃにするので、なぜそれが問題なのかをトラブルシューティングする必要があります。パイプで区切られたファイルを.csvファイルに変換するために使用したコードを見てみましょうか?ありがとうございます – gymcode
http://stackoverflow.com/questions/8061307/rows-messed-up-when-converting-pipe-delimited-text-files-to-csv-excel-file-c-sh これはエラーです私は現在直面している、私はそれに変換にエラーがあった理由を手掛かりがありません。 – gymcode