SQLiteDataAdapter converts empty value to 0 - how

2019-09-02 03:32发布

问题:

Below is a snippet of the code. As you can see, that method returns a table from SQLite database, and adds that table to a DataSet if it doesn't exist yet.

SQLiteConnection connection;
DataSet Set = new DataSet();

DataTable GetTable(string tableName, string command)
{
    if (!Set.Tables.Contains(tableName))
    {
        var adapter = new SQLiteDataAdapter(command, connection);
        SQLiteCommandBuilder builder = new SQLiteCommandBuilder(adapter);

        adapter.FillSchema(Set, SchemaType.Source, tableName);
        adapter.Fill(Set, tableName);
        adapter.Dispose();
    }

    return Set.Tables[tableName];
}

To call it, for example

DataTable myTable = GetTable("MyTable", "select * from MyTable);

To access a field:

object emptyValue = myTable.Rows[0]["Some_Column"];

There are some cells in the SQLite file that are of type INT, and their values are empty (not null). However when I'm trying to populate myTable, they are conveniently converted to 0's which I DO NOT WANT. How do I go about fixing that? I would like to keep empty values (and null values) as null's when importing to C#.

You can retrieve the row I was talking about above by executing the following SQL statement:

select * from MyTable where some_column = ''

The SQLite file that I use is SQLite3. Just in case it helps.

Thanks in advance!