SQLiteDataAdapter converts empty value to 0 - how to prevent that?

牧云@^-^@ 提交于 2019-12-11 20:37:23

问题


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!

来源:https://stackoverflow.com/questions/29358034/sqlitedataadapter-converts-empty-value-to-0-how-to-prevent-that

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!