开发者

SQLITE problem in rowid

I am using SQLite C# library and as per my requirement, before going to add I am retreving the rowid of that table. It is working fine but hetting error when table is empty. Once we add any data(a row) then it’s working fine .

        mDbCon  = GetConnection();
        SQLiteCommand cmd = mDbCon.CreateCommand();
        cmd.CommandTex开发者_运维技巧t = " SELECT MAX(rowid) FROM " + “MYTABLE”;
        cmd.CommandType = CommandType.Text;
        mDbCon.Open();
        SQLiteDataReader sqReader = cmd.ExecuteReader();
        while (sqReader.Read())
        {
            if ( sqReader.IsDBNull(0) )
            {
                max = (Int32)sqReader.GetInt32(0);
            }
        }
        mDbCon.Close();

It’s throwing exception when table “MYTABLE” don’t have any data.


This should work for you.

SELECT IFNULL(MAX(RowId), 1) AS Id FROM MYTABLE


try int maxRowID = Convert.Int32(cmd.ExecuteScalar());

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜