UWP SQLite - 返回null但表中的数据

时间:2018-01-06 20:11:08

标签: sqlite uwp uwp-xaml

我有数据库(显示在屏幕上) data

但返回的值是null?怎么可能,在输出中我得到了这个:

output

这是我使用的代码:

public static SQLiteConnection dbConnection = new SQLiteConnection("RPR_REKENSOFTWARE_DB.db");
public static List<String> GetParts()
        {
            var items = new List<String>();
            try
            {
                string sSQL = @"SELECT * FROM parts;";
                ISQLiteStatement dbState = dbConnection.Prepare(sSQL);
                // Get the records
                while (dbState.Step() == SQLiteResult.ROW)
                {
                    // Say what it is.
                    string partNr = dbState[1] as string;

                    items.Add(partNr);
                }
                return items;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw ex;
            }
        }

这是我使用的数据库:

db structure

1 个答案:

答案 0 :(得分:0)

原因是您正在将数据库中的值转换为string,尽管它实际上是int。你应该这样做:

string partNr = ( ( int )dbState[ 1 ] ).ToString();

或更简单:

string partNr = dbState[ 1 ].ToString();

原因是从数据库返回的行包含.NET列类型的.NET等价物,因此dbState[1]int。但是,当你在int上使用as string时,它无法投射,你得到null