- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 SQLite ADO .Net (sqlite.phxsoftware.com) 中,ExecuteScalar() 查询方法的文档如下:“执行命令并返回结果集第一行的第一列(如果存在),如果没有结果集则返回 null被退回了。”。我创建了一个表:
create table users ( id integer primary key, name text )
并使用 ExecuteScalar() 执行一个队列:
select ( id ) from users where name = @name
但是,很奇怪——我不能将返回值转换为“int”,只能转换为“long”!为什么会这样,'id'字段在数据库中被定义为'integer',而不是'bigint'?
最佳答案
这是因为 SQLite“INTEGER PRIMARY KEYS”实际上不是整数,而是长整数(64 位整数)。来自documentation :
Every row of every SQLite table has a 64-bit signed integer key that is unique within the same table. This integer is usually called the "rowid". The rowid is the actual key used in the B-Tree that implements an SQLite table. Rows are stored in rowid order. The rowid value can be accessed using one of the special names "ROWID", "OID", or "ROWID".
If a column is declared to be an INTEGER PRIMARY KEY, then that column is not a "real" database column but instead becomes an alias for the rowid. Unlike normal SQLite columns, the rowid must be a non-NULL integer value. The rowid is not able to hold floating point values, strings, BLOBs, or NULLs.
An INTEGER PRIMARY KEY column is an alias for the 64-bit signed integer rowid.
这是 SQLite 的 dynamic typing mechanism 的副作用.
实际上,要回答您的问题,您应该检索尽可能长的值并尽可能使用它,如果该值适合 32 位,则仅使用转换运算符或 Convert 类。因为它恰好是一个 ID,所以您不需要将其转换或强制转换为 32 位整数。
关于SQLite ADO .NET 和 ExecuteScalar(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1810829/
我正在尝试返回 DbSet哪里Survey定义为 public class Survey { public int Id { get; set; } [StringLength(100
我使用库“Dapper”连接 MS Sql。 当我调用存储过程时,它显示错误信息 can not find stored procedure. 但是存储过程它已经存在于数据库中。我该如何解决这个问题?
给我这个错误连接必须有效且打开我不知道如何解决它。 private void loginUtenti(string user, string psw) { create
我试图在一个 int 变量中获取 sql 查询的结果,但我收到了对象空引用错误。有人可以指导我吗。 oconn = new SqlConnection(oSession.CONNECTION_STR
我有这个模块 strSQL = "Insert Into TblInsurancePersonel (CompId,PersonelNo,FirstName,LastName) "; st
下面的代码有点问题。 NextIPID() 方法应该简单地查看 SQL Server 中的一个表,该表返回字段 IPID 的最大值,然后将其加 1,以获得序列中的下一个数字。 public int N
我正在使用以下 SQL 查询和 ExecuteScalar() 方法从 Oracle 数据库中获取数据: sql = "select username from usermst where useri
我正在尝试使用 ExecuteScalar() 从数据库中返回一个整数。但是,当我在数据库本身上运行查询时,我得到了正确的答案,并且 c# 始终给我一个 0(Null)。我知道它返回一个空值,因为在我
ExecuteScalar会自动关闭连接吗? 最佳答案 不,使用 ExecuteScalar() 时需要显式打开和关闭连接。 关于ADO.NET:ExecuteScalar 是否自动关闭连接?,我们在
我正在使用 SQL Server 2005 和 C#。我想在向数据库中插入记录时获取主键值。 为此,我有一个 C# 方法,我可以在其中插入和更新记录。我正在使用 ExecuteScalar 方法进行插
我正在使用 ExecuteScalar 获取一列: cmd.commandtext = "select rodeuser from customer_db_map"; string rode
ExecuteScalar() 有时返回空对象 - 而不是 null- 尽管记录存在。当我用 quickwatch 分析这个对象时,我看到 object.GetType() 等于 DbNull。我可以
我在同一个 ExecuteScalar 命令中同时检查 2 条记录时遇到问题。 我想在本地数据库中不同列的同一行中一起搜索2条记录,我尝试像下面的代码一样进行操作,但只有在1个条件下执行它才可以,但我
关键是我有这段代码 var ojv = xmd.ExecuteScalar().ToString(); if (ojv != null) { C
当我运行以下代码时: query = "select count(*) from table where name = '?name'"; MySqlConnection connection =
我有一个使用事务的 sql 查询,它使用 return 来指示失败或成功,如下所示: BEGIN DECLARE @intErrorCode INT; DECLARE @emailAu
下面是我的程序,用于测量 ExecuteScalar 进行多次迭代所花费的时间。 static void Main(string[] args) { string sqlConnectionS
我正在用可视化 C# 编写一个程序,我在其中提取数据并将其放入 dataGridView 中。这一切都很好,但如果我之后尝试提取更多数据(我想将数据放入文本框),它会出现错误。 这是我提取数据并将其放
我试过各种方法,但似乎都行不通。由于结果为空,例程一直返回 0。 这是我的代码: string strSql = "SELECT [ID] FROM [PurchaseOrder].[dbo].[FM
我正在使用 Xamarin 中的 MySQL 库连接到我的数据库。我正在调用 ExecuteScalar() 命令来检查我的数据库中是否存在用户。 我将 ExecuteScalar() 的返回转换为
我是一名优秀的程序员,十分优秀!