为什么我的原始查询计数行总是返回-1? [英] Why does my raw query to count rows always returns -1?

查看:81
本文介绍了为什么我的原始查询计数行总是返回-1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查表是否存在,但不能正常工作。

I'm trying to check if table exists, but not working correctly.

由于某种原因,count总是返回-1。我已经在数据库中有一个表。
应该返回1吗?

For some reason, count always returns -1. I already have a table in the database. It should return 1?

SearchEntities db = new SearchEntities();

var qry3 = "";
var sql4 = "SELECT Count(*) FROM SearchDB.INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'portland'";
var count = db.Database.ExecuteSqlCommand(sql4);


推荐答案

ExecuteSqlCommand 不返回数据,它始终返回 Int32 ,这是SQL脚本在<$ c $时处理的行数c> DDL / DML 命令。

ExecuteSqlCommand doesn't return data, it always returns an Int32, which is the number of rows processed by the SQL script when it's a DDL/DML command.

您要 SqlQuery< TElement>(字符串,对象[])

You want SqlQuery<TElement>(String, Object[]) instead.

var count = db.Database.SqlQuery<int>(sql4).Single();

这篇关于为什么我的原始查询计数行总是返回-1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆