Click here to Skip to main content
15,178,226 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
I am using Entity framework to execute sql query using entity framework and Successfully executed using
C#
context.Database.ExecuteSqlCommand(strt)  
It takes sql query, now i want to execute sqlcommand using entity framework. How i can do it?

My Code Is Below

C#
while (reader.Read())
               {
                   StringBuilder MyStringBuilder = new StringBuilder("Insert into " + Tablename + "(");
                   StringBuilder CopyString = new StringBuilder("Insert into " + Tablename + "_Temp (");

                   for (int k = 1; k < validList.Count; k++)
                   {
                       MyStringBuilder.Append("@" + validList[k].ToString()).Append(",");
                       CopyString.Append("@" + validList[k].ToString()).Append(",");
                   }

                   MyStringBuilder.Remove(MyStringBuilder.Length - 1, 1);
                   CopyString.Remove(CopyString.Length - 1, 1);

                   MyStringBuilder.Append(")");
                   CopyString.Append(")");

                   SqlCommand MountCmd = new SqlCommand(MyStringBuilder.ToString(),Mcon);

                   ParamArrayAttribute
                   for (int a = 1; a < validList.Count; a++)
                   {
                       s = reader[a].ToString();
                       MountCmd.Parameters.AddWithValue("@"+validList[a].ToString()+"", s);
                   }
                   try
                   {
                       context.Database.ExecuteSqlCommand();       //here i am trying to excute SqlCommand Object MountCmd
                   }
                   catch (Exception)
                   {
                       context.Database.ExecuteSqlCommand();
                   }
               }
Posted
Updated 8-Oct-13 1:01am
v3
Comments
Alexander Dymshyts 8-Oct-13 5:42am
   
Try to look for an answer in google, im sure you will find it there
Vi(ky 8-Oct-13 5:49am
   
i am trying bro but ExecuteSqlCommand() only takes sql query and Params Object array.
still searching

List<sqlparameter> parameters = new List<sqlparameter>();
C#
for (int a = 1; a < validList.Count; a++)
{
s = reader[a].ToString();
parameters.Add(new SqlParameter("@" + validList[a].ToString() + "", s));                       
}                                   
context.Database.ExecuteSqlCommand(MyStringBuilder.ToString(),parameters.ToArray());
context.SaveChanges();
   
Hi,
Try like this, its an update query using ExecuteSqlCommand
C#
string name = "tanweer";
       long id = 10001;
       var sql = @"Update [User] SET FirstName = {0} WHERE Id = {1}";
       ctx.Database.ExecuteSqlCommand(sql, name, id);
   
Comments
Vi(ky 8-Oct-13 6:04am
   
Thank You Tanveer to reply but the number of coulmn is not predefined and it is defined at run time
cabarnguyen 28-Apr-16 3:55am
   
tks
cabarnguyen 28-Apr-16 3:56am
   
tks
cabarnguyen 28-Apr-16 3:56am
   
tks
tanweer 8-Oct-13 6:13am
   
try the below link for more results
http://www.entityframeworktutorial.net/EntityFramework4.3/raw-sql-query-in-entity-framework.aspx

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