0

VS2005 C#とSQL Server 2005を使用しています。現在、.CSVファイルデータをSQL Serverデータベースにインポートしています。C#VS2005クエリ式の構文エラー(演算子がありません)

私はいくつかのエラーが発生しています。これは私のSQL文に関連していると想定しています。以下は私のコードです: "クエリ式の構文エラー(演算子がありません) '[説明] FROM20111109164041.csv'"

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 dir = @"C:\Documents and Settings\rhlim\My Documents\Visual Studio 2005\WebSites\SoD\UploadFiles\"; 
      string mycsv = DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension; 
      // Save the Excel spreadsheet on server. 
      FileUpload1.SaveAs(dir+mycsv); 

      // Create Connection to Excel Workbook 
      string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + ";Extended Properties=Text;"; 
      using (OleDbConnection ExcelConnection = new OleDbConnection(connStr)) 
      { 
       OleDbCommand ExcelCommand = new OleDbCommand("SELECT [TABLES] FROM" + mycsv, 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=<UID>;Password=<PW>"; 

       // 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>"); 
    } 

私はエラー

を取得しています

(DbDataReader dr = ExcelCommand.ExecuteReader())を使用して実行しています。説明は私のデータベースの最後の列です。

誰かが自分のコードに何が間違っているかを知っていますか?あなたは

enter image description here

答えて

4

あなたからとcsvファイルの間のスペースを必要とするありがとうございました! :)私はいつもString.Formatのメソッドを使用する理由です

OleDbCommand ExcelCommand = new OleDbCommand("SELECT [TABLES] FROM " + mycsv, ExcelConnection); 

、あなたは最終的に文字列がどのように見えるかはるかに良い参照してください。あなたがFROMとあなたの間の空白を配置する必要があるよう

OleDbCommand ExcelCommand = new OleDbCommand(string.Format("SELECT [TABLES] FROM {0}",mycsv), ExcelConnection); 
2

らしいですCSV-ファイル: '20111109164041.csv FROM [説明]'

+0

私はあなたが大好き!ありがとう – gymcode

0

あなたは、単一引用符の間に配置しなければならない文字列を追加する場合は

OleDbDataAdapter da = new OleDbDataAdapter("select * , '" + tempUid + "' as [UID] from [" + sheet1 + "]", conn); 
関連する問題