views:

92

answers:

1

Hi,

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,val36,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?

A: 

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()

Salil
Thanks,that seems to be working after I updated 'val0' to '@val0'...But why doesnt it accept the parameter as 'val0'?Mysql accepts both 'val0' and '@val0'.right?
kranthi
one more thing is that its taking about a minute to insert all the data into database.is there a way I can reduce the time consumed? and is there I can get the datatypes of the columns from excel sheet?Please help..
kranthi