Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to passing data from .xls file to sql server but its still not working the error messages says Keyword not support metadata, I'm using Entity Framework its my code;

C#
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string conn = ConfigurationManager.ConnectionStrings["BaruEntities"].ConnectionString;
                SqlConnection con = new SqlConnection(conn);
                string query = "Insert into TrashExcel(No Pin,Nama, No HP,Tgl Lahir, No KTP) Values('" + ds.Tables[0].Rows[i][0].ToString() + "','" + ds.Tables[0].Rows[i][1].ToString() + "','" + ds.Tables[0].Rows[i][2].ToString() + "','" + ds.Tables[0].Rows[i][3].ToString() + "','" + ds.Tables[0].Rows[i][4].ToString() + "')";
                con.Open();
                SqlCommand cmd = new SqlCommand(query, con);
                cmd.ExecuteNonQuery();
                con.Close();
}


If you have solution for this please tell me and I'll appreciate it Thanks
Posted

Use parameters and brackets when you have spaces in the column names
C#
string query = "Insert into TrashExcel([No Pin],[Nama], [No HP],[Tgl Lahir], [No KTP]) Values(@p1,@p2,@p3,@p4,@p5)";
using(SqlConnection con = new SqlConnection(conn))
{
 con.Open();
 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
 {
  using(SqlCommand cmd = new SqlCommand(query, con))
  {
    cmd.Parameters.AddWithValue("@p1", (string)ds.Tables[0].Rows[i][0]);
    cmd.Parameters.AddWithValue("@p2", (string)ds.Tables[0].Rows[i][1]);
    cmd.Parameters.AddWithValue("@p3", (string)ds.Tables[0].Rows[i][2]);
    cmd.Parameters.AddWithValue("@p4", (string)ds.Tables[0].Rows[i][3]);
    cmd.Parameters.AddWithValue("@p5", (string)ds.Tables[0].Rows[i][4]);
    cmd.ExecuteNonQuery();
  }
 }
}
 
Share this answer
 
Firstly u are not using entity framework . U are using Ado.net
And there is a library that you can find as a nuget packets named LinqtoExcel

With this library you can use Every sheets in excel as a table like in Sql
and every row as an entity.
 
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