0

I'm trying to execute below code and it get executed correctly when I put type as object, but I don't know how to read data from the result. Is there a any other way to do this?

string sql = "select * from sys.columns where Name = N'@columnName' and Object_ID = Object_ID(N'@tableName')";
var args = new DbParameter[] { new SqlParameter { ParameterName = "columnName", Value = "Name2" }, new SqlParameter { ParameterName = "tableName", Value = "OrderDetails" } };
var students = ctx.ExecuteStoreQuery<object>(sql, args);
janitheshan
  • 325
  • 2
  • 11
  • 25

2 Answers2

0

If you have a concrete type that can hold the results of the query I think you can do this:

List<Student> students = new List<Student>(); 
students = ctx.ExecuteStoreQuery<student>(sql, args);

then loop through the list

foreach (Student student in students)
{
        Console.WriteLine(student.Name); //or whatever
}
Neil Thompson
  • 6,356
  • 2
  • 30
  • 53
  • No I don't have a concrete type that's the issue. Because I'm trying to execute below query `select * from sys.columns where Name = N'@columnName' and Object_ID = Object_ID(N'@tableName')` I need to check is the column exist on the given table. – janitheshan Jan 22 '14 at 10:49
  • 1
    ah yes, the 'unknown type' part of the title should have tipped me off. my bad. AFAIK EF is really designed to work with types. If you don't want to make one can you not just do an ADO.NET request or have EF return a DataTable as per Polla Tosen's post here: http://stackoverflow.com/questions/4586834/returning-a-datatable-using-entity-framework-executestorequery?rq=1 – Neil Thompson Jan 22 '14 at 10:53
  • Yes Neil, I think that's the only way to do it. Thanks for the help. – janitheshan Jan 22 '14 at 14:10
0

this is the solution that I came up with

string sql = "select * from sys.columns where Name = @columnName and Object_ID = Object_ID(@tableName)";

DataSet ds= new DataSet();
EntityConnection entityConn = (EntityConnection)ctx.Connection;
SqlConnection sqlConn = (SqlConnection)entityConn.StoreConnection;
SqlCommand cmd = new SqlCommand(sql, sqlConn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
using (cmd)
{
       SqlParameter Prm = new SqlParameter("tableName", "OrderDetails");
       cmd.CommandType = CommandType.Text;
       cmd.Parameters.Add(Prm);
       da.Fill(ds);
 }
janitheshan
  • 325
  • 2
  • 11
  • 25