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 'D:\Book11.csv INSERT INTO TABLE sla1 FIELDS TERMINATED BY ','LINES TERMINATED BY' at line 1.
My code is given below.......
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>");
}
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)";
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, exclecon);
exclecon.Open();
mycom.ExecuteNonQuery();
myCUpload.ExecuteNonQuery();
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('File uploaded Successfully ')</script>");
}
catch (Exception ex)
{
Label4.Text = "Error: " + ex.Message.ToString();
}
}
}