0

I am using VS2005 C# and SQL Server 2005. I am currently doing an import for a .CSV excel file data into my SQL Server database.

I am having some error, which I assume is related to my sql statement. Below is my code:

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

I am getting the error

"Syntax error (missing operator) in query expression '[Description] FROM20111109164041.csv'."

while executing using (DbDataReader dr = ExcelCommand.ExecuteReader()). Description is the last column in my database.

Anyone know what is wrong with my code? Thank You

enter image description here

3 Answers 3

4

You need a space between FROM and the csv file! :)

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

That's why I always use the string.Format method, you see much better how the final string will look:

OleDbCommand ExcelCommand = new OleDbCommand(string.Format("SELECT [TABLES] FROM {0}",mycsv), ExcelConnection);
Sign up to request clarification or add additional context in comments.

Comments

2

Seems like you need to place a blank between FROM and your CSV-File: '[Description] FROM 20111109164041.csv'

Comments

0

If you are adding a string you should place it between single quotes

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.