Click here to Skip to main content
15,905,566 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I want to insert csv file data in mysql database table using asp.net.
There is two table(id,month,year,contenttype) and in 2nd table(name ,age,sex,class,id)they are related with foreigen key.

When i insert record then it given following error on webpage...
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LOAD DATA INFILE '+path+' INSERT INTO TABLE sla1 FIELDS TERMINATED BY ',' LINES ' at line 1

my code given below..

C#
MySqlConnection exclecon = new MySqlConnection("Server=Localhost;DataBase=password1;user=root;password=nectar");
            string path = FileUpload1.PostedFile.FileName;
            string ext = Path.GetExtension(path);
            string contenttype = string.Empty;
            if (!FileUpload1.HasFile)
            {

                ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Please select a file')</script>");  //if file uploader has no file selected

            }
            else if (FileUpload1.HasFile)
            {
                try
                {
                    switch (ext)
                    {
                        case ".csv":

                            contenttype = "application/vnd.ms-excel";

                            break;
                    }
                    string year = ddlyear.SelectedItem.ToString();
                    string month = ddlmonth.SelectedItem.ToString();
                    string insquery = "insert into sla(month,year,contenttype) values(@month,@year,@contenttype) LOAD DATA INFILE '+path+' INSERT INTO TABLE sla1 FIELDS TERMINATED BY ',' LINES TERMINATED BY '\\n' IGNORE 2 LINES";
                    MySqlCommand mycom = new MySqlCommand(insquery, exclecon);

                    mycom.Parameters.AddWithValue("@month", month);

                    mycom.Parameters.AddWithValue("@year", year);
                    mycom.Parameters.Add("@contenttype", MySqlDbType.VarChar).Value = contenttype;
                    exclecon.Open();
                    mycom.ExecuteNonQuery();
                    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('File uploaded Successfully ')</script>");
                }

                catch (Exception ex)
                {
                    Label4.Text = "Error: " + ex.Message.ToString();

                }
            }
        }
Posted
Updated 18-Apr-13 6:56am
v2

1 solution

Hello,

Try executing them as two separate SQL's. For LOAD DATA to successfully work make sure that the file is transferred to the server on which MySQL daemon is running and that it has the read permissions for the folder in which file resides. The path of the file either needs to be the absolute path or should be a path relative to servers data directory or the database directory of the default database. If you don't want to transfer file to server then you will have to add keyword LOCAL in your SQL. The LOCAL keyword will not work if MySQL is not started with --local-infile=0. More information on this can be found here[^]. Your query execution code might look very similar to one shown below.
C#
MySqlConnection exclecon = new MySqlConnection("Server=Localhost;DataBase=password1;user=root;password=nectar");
string insquery = "INSERT INTO sla (month, year, contenttype) VALUES(@month, @year, @contenttype)";
string uploadQry = "LOAD DATA INFILE " + path + " INSERT INTO TABLE sla1 FIELDS TERMINATED BY ',' LINES TERMINATED BY '\\n' IGNORE 2 LINES";
MySqlCommand mycom = new MySqlCommand(insquery, exclecon);
mycom.Parameters.AddWithValue("@month", month);
mycom.Parameters.AddWithValue("@year", year);
mycom.Parameters.Add("@contenttype", MySqlDbType.VarChar).Value = contenttype;

MySqlCommand myCUpload = new MySqlCommand(uploadQry, excelcon);

exclecon.Open();
mycom.ExecuteNonQuery();
myCUpload.ExecuteNonQuery();

Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900