在SQL Server中,我创建了一个存储过程,该存储过程使用Open Query触发一条简单的select语句(在SET NOCOUNT OFF的情况下),以从链接服务器查询数据。在SQL Server中查询此数据时,会返回正确的结果,例如:
SELECT * FROM OPENQUERY(SERVER, ''SELECT * FROM db.table WHERE field = ' + '''' + '''' + @var+ '''' + ''''')'
现在,我有了一个使用实体框架v4访问数据的C# WinForms应用程序,并希望在代码中访问此存储过程。
我执行了通常的“从数据库更新模型”,并添加了存储过程,然后选择添加一个函数导入(例如,名为getData )。然后,我注意到,在单击'Get Column Information‘之后,我收到了以下消息:
'The selected stored procedure returns no columns'
此时,我单击了OK,然后编写了一些简单的代码来访问SP (如下所示):
using(var context = new MyContext())
{
var result = context.getData('paramdata');
}
单步执行代码时,result设置为'-1‘。在做了一些阅读之后,有人建议在存储过程中将NOCOUNT设置为OFF,我这样做了,但没有什么区别。
我不确定为什么这在SQL Server中的查询中有效,但在实体框架中不起作用?
有没有办法让它起作用?
提前谢谢。
发布于 2012-09-24 12:37:53
因为您使用的是OpenQuery,所以列列表对于SQL server是未知的。
尝试创建一个表变量,将openquery的结果插入其中,然后从表变量中进行选择。
declare @t table(ID int, value nvarchar(50))
insert @t (ID, value)
select Id,Value FROM OPENQUERY(SERVER, 'SELECT * FROM db.table')
select ID, Value from @t
发布于 2012-09-24 11:45:06
您的查询不返回任何数据,因此不返回任何行信息。尝试修复您的查询并检查您正在运行的数据库。修复您选择*,这对于查询数据库来说是一个非常糟糕的模式。
发布于 2014-05-19 15:11:47
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @Query nvarchar(max);
DECLARE @LinkServer varchar(20);
DECLARE @CatalogSch varchar(20);
DECLARE @DIVISION_NO INT;
DECLARE @BLOCK_NO INT;
DECLARE @LOT_NO INT
/* With this code I get the server and catalog (production or develpment environment)*/
EXEC [dbo].[usp_GetLinkServerAndSchema] 'ACTJO', @LinkServer out, @CatalogSch out;
/* Declare the table */
declare @t table(Division_no int, block_no int, lot_no int)
/* Build the query to get the three values */
SET @Query = 'SELECT @DIVISION_NO = [DIVISION_NO], @BLOCK_NO = [BLOCK_NO], @LOT_NO = [LOT_NO] FROM OPENQUERY ('+@LinkServer+',''
SELECT DIVISION_NO, BLOCK_NO, LOT_NO
FROM '+@CatalogSch+'.[CSS_PREMISE]
WHERE ACCOUNT_NO = '+convert(varchar,@account)+''')'
/* execute the query */
EXEC sp_executesql @Query, N'@DIVISION_NO INT output, @BLOCK_NO INT output, @LOT_NO INT output', @DIVISION_NO output, @BLOCK_NO output, @LOT_NO output;
/* insert the values into the table */
insert @t (Division_no,block_no,lot_no)
select @DIVISION_NO, @BLOCK_NO, @LOT_NO;
/* query the temporary table */
select Division_no,block_no,lot_no from @t
https://stackoverflow.com/questions/12564327
复制相似问题