本文介绍了LINQ to SQL中的动态查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从linq到sql的字符串中发送查询

I am sending query in a string from linq to sql

string _ObjCocktail = "declare @max_id int Insert into dbo.Cocktails(name,cocktail_category_id,instrustions) values ('Mojto',1,'Instruction of the cocktail') select @max_id = MAX(dbo.Cocktails.id) from dbo.Cocktails Insert into RecipeDetails(cocktail_id,mixers_id,liquor_id,quantity,unit_id,other_ingredients_id)  values  ( @max_id, null,2,null,1,null) Insert into RecipeDetails(cocktail_id,mixers_id,liquor_id,quantity,unit_id,other_ingredients_id)  values  ( @max_id, null,7,null,1,null)";
public void InsertCocktail()
        {
            try
            {
                 _DataContext.InsertCocktails(_ObjCocktail);
            }
            catch (Exception ex)
            {

            }



我正在像这样在sql中处理该字符串.

and

i am handling that string in sql like this.

ALTER procedure [dbo].[InsertCocktails]
(
@query varchar(max)
)
as
begin
exec @query
end

此错误将导致异常.

It is going to Exception with this error.

The name 'declare @max_id int Insert into dbo.Cocktails(name,cocktail_category_id,instrustions) values ('Mojto',1,'Instruction of the cocktail') select @max_id = MAX(dbo.Cocktails.id) from dbo.Cocktails Insert into RecipeDetails(cocktail_id,mixers_id,liquor_id,quantity,unit_id,other_ingredients_id)  values  ( @max_id, null,2,null,1,null) Insert into RecipeDetails(cocktail_id,mixers_id,liquor_id,quantity,unit_id,other_ingredients_id)  values  ( @max_id, null,7,null,1,null)' is not a valid identifier.


当我复制将此查询字符串粘贴到sql中时,它运行良好.


When i copy paste this query string in sql it is running fine.

推荐答案

exec @query




to

exec (@query)


这篇关于LINQ to SQL中的动态查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 22:21