【问题标题】:C# VS2005 Syntax error (missing operator) in query expression查询表达式中的 C# VS2005 语法错误(缺少运算符)
【发布时间】:2011-12-25 03:37:19
【问题描述】:

我正在使用 VS2005 C# 和 SQL Server 2005。我目前正在将 .CSV excel 文件数据导入我的 SQL Server 数据库。

我有一些错误,我认为这与我的 sql 语句有关。以下是我的代码:

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

我收到了错误

“查询表达式'[描述] FROM20111109164041.csv'中的语法错误(缺少运算符)。”

使用 (DbDataReader dr = ExcelCommand.ExecuteReader()) 执行时。描述是我数据库中的最后一列。

有人知道我的代码有什么问题吗?谢谢你

【问题讨论】:

    标签: c# sql-server visual-studio sql-server-2005 visual-studio-2005


    【解决方案1】:

    FROM 和 csv 文件之间需要一个空格! :)

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

    这就是为什么我总是使用 string.Format 方法,你会更好地看到最终字符串的外观:

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

    【讨论】:

      【解决方案2】:

      似乎您需要在 FROM 和 CSV 文件之间放置一个空白: '[描述] FROM 20111109164041.csv'

      【讨论】:

        【解决方案3】:

        如果你要添加一个字符串,你应该把它放在单引号之间

        OleDbDataAdapter da = new OleDbDataAdapter("select * , '" + tempUid + "' as [UID] from [" + sheet1 + "]", conn);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-01-14
          • 1970-01-01
          • 2018-09-02
          • 2018-01-29
          • 2012-11-14
          相关资源
          最近更新 更多