C#VS2005查询表达式中的语法错误(缺少运算符)

时间:2011-11-09 09:18:40

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

我正在使用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())。说明是我的数据库中的最后一列。

任何人都知道我的代码有什么问题?谢谢

enter image description here

3 个答案:

答案 0 :(得分:4)

您需要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);

答案 1 :(得分:2)

好像你需要在FROM和你的CSV文件之间留一个空白: '[描述] FROM 20111109164041.csv'

答案 2 :(得分:0)

如果要添加字符串,则应将其放在单引号

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