【问题标题】:Retrieve data using Dynamic Query and Linq to SQL使用 Dynamic Query 和 Linq to SQL 检索数据
【发布时间】:2011-02-04 22:17:23
【问题描述】:

您好,我有一个非常复杂的动态查询 我想用来从数据库中检索数据 我在 .net 3.5 sql server 2008 中工作

我创建了一个接受 varchar(max) 作为输入参数的存储过程

execute (@SqlQuery)

它执行但不返回任何东西

我真的很想使用 LINQ,因为我所有的项目都是使用 linq 实现的

任何想法如何做到这一点

有什么问题?

【问题讨论】:

    标签: sql linq dynamic


    【解决方案1】:

    如果您想通过 LINQ 执行原始 SQL,您应该查看方法 ExecuteQuery<T>

    【讨论】:

      【解决方案2】:
          using (SqlConnection con = new SqlConnection("server=(local)\\SQLEXPRESSdatabase=MyDatabase;Integrated Security=SSPI"))
          {
              using (SqlCommand cmd = new SqlCommand())
              {
      
                  cmd.Connection = con;
                  cmd.CommandText = @sqlcommand actualtext;
      
                  cmd.Parameters.Add(anyParams that are in the query);
                  con.open();
                  SqlDataReader rdr = cmd.ExecuteReader();
      
                  if (rdr.HasRows)
                  {
                     rdr.Read();
                     //code here reader should have all the data returned that met the select statement
                  }
      
              }
          }        
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多