Problem passing dynamic parameters to a mysql query from asp.net page
I've a requirement that I need to read an excel sheet programmatically using asp.net/C# and write the data obtained into a mysql table.The excel sheet contains something around 50 columns and 2000 records.I am able to read the data from the excel sheet and store it in a dataset.I am using the following code to write the data into mysql table.
for (int i = 1; i <= myDataSet1.Tables[0].Rows.Count - 1; i++)
{
MySqlCommand cmd = new MySqlCommand();
for (int j = 0; j <= myDataSet1.Tables[0].Columns.Count - 1; j++)
{
paramset[j] = myDataSet1.Tables[0].Rows[i][j].ToString();
cmd.Parameters.AddWithValue("val" + j, paramset[j]);
}
cmd.CommandText = "Insert into faqimport values(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10,val11,val12,val13,val14,val15,val16,val17,val18,val19,val20,val21,val22,val23,val24,val25,val26,val27,val28,val29,val30,val31,val32,val33,val34,val35,val3开发者_C百科6,val37,val38,val39,val40,val41,val42)";
cmd.Connection=con;
cmd.CommandType = CommandType.Text;
int x=cmd.ExecuteNonQuery;
}
When I try to run the above code I am getting 'Unknown column 'val0' in 'field list'' error.I understand that i am manually creating the parameters val0,val1,val2.....instead I am creating them dynamically using the integer 'j'.But I do not want to create around 50 parameters to insert data into the database table.
Could someone please tell me if there is a wayaround this?
Also is there a way i can get the column datatypes from the excel sheet in order to create a new table in mysql with the columns in the excel sheet?
I think the "val0, val1.." are not getting replaced with the parameters you are adding to the command. Could it be because you are not prefixing them with an '@'? ("@val0, @val1..") Same for the AddWithValue()
精彩评论