SQLite / Mono trouble ?

  • Thread starter Thread starter Rick van Lieshout
  • Start date Start date
R

Rick van Lieshout

Hi all,

I'm trying to use the sql engine SQLite within my Visual C# project. I
downloaded the mono version of the .NET framework so I could use
Mono.Data.SqliteClient.dll , however I can't seem to be able to use the
IDataReader object. Any suggestions ?

Complete work directory:
http://home.wanadoo.nl/hgvl/Copy of SqliteTest.zip

This is my code:
-----------------------------
using System;
using System.Data;
using Mono.Data.SqliteClient;

namespace SqliteTest
{
/// <summary>
/// Summary description for SqliteTestApp.
/// </summary>
class SqliteTestApp
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string connectionString = "URI=file:SqliteTest.db";
IDbConnection dbcon;
dbcon = new SqliteConnection(connectionString);
dbcon.Open();
IDbCommand dbcmd;
dbcmd = dbcon.CreateCommand();
dbcmd.Connection = dbcon;
string sql = "CREATE TABLE test (nummer int)";
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
sql = "INSERT INTO test VALUES (12)";
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
sql = "SELECT nummer FROM test";
dbcmd.CommandText = sql;
IDataReader reader;
reader = dbcmd.ExecuteReader();
while (reader.Read())
{
int nummer = reader.GetInt32(0);
Console.WriteLine(nummer);
}
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbcon.Close();
dbcon = null;
}
}
}

Thanks in advance,

Rick van Lieshout
 
Don't create the reader on one line then set it the next. Instead you create
it as a returned result on the same line:

don't do:
IDataReader reader;
reader = dbcmd.ExecuteReader();

Do this instead:
IDataReader reader = dbcmd.ExecuteReader();


Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
Sorry, this didn't do the trick.. I guess it is the callback function.. I'll
try a older DLL as soon as i can access the sqlite.org site again.



Mark Fitzpatrick said:
Don't create the reader on one line then set it the next. Instead you create
it as a returned result on the same line:

don't do:
IDataReader reader;
reader = dbcmd.ExecuteReader();

Do this instead:
IDataReader reader = dbcmd.ExecuteReader();


Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage


Rick van Lieshout said:
Hi all,

I'm trying to use the sql engine SQLite within my Visual C# project. I
downloaded the mono version of the .NET framework so I could use
Mono.Data.SqliteClient.dll , however I can't seem to be able to use the
IDataReader object. Any suggestions ?

Complete work directory:
http://home.wanadoo.nl/hgvl/Copy of SqliteTest.zip

This is my code:
-----------------------------
using System;
using System.Data;
using Mono.Data.SqliteClient;

namespace SqliteTest
{
/// <summary>
/// Summary description for SqliteTestApp.
/// </summary>
class SqliteTestApp
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string connectionString = "URI=file:SqliteTest.db";
IDbConnection dbcon;
dbcon = new SqliteConnection(connectionString);
dbcon.Open();
IDbCommand dbcmd;
dbcmd = dbcon.CreateCommand();
dbcmd.Connection = dbcon;
string sql = "CREATE TABLE test (nummer int)";
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
sql = "INSERT INTO test VALUES (12)";
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
sql = "SELECT nummer FROM test";
dbcmd.CommandText = sql;
IDataReader reader;
reader = dbcmd.ExecuteReader();
while (reader.Read())
{
int nummer = reader.GetInt32(0);
Console.WriteLine(nummer);
}
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbcon.Close();
dbcon = null;
}
}
}

Thanks in advance,

Rick van Lieshout
 
Don't create the reader on one line then set it the next. Instead you
create it as a returned result on the same line:

don't do:
IDataReader reader;
reader = dbcmd.ExecuteReader();

Do this instead:
IDataReader reader = dbcmd.ExecuteReader();

and why would that help? The first of his 2 lines declares an
identifier in the local stackframe. The second line puts the reference
address of the reader object as the value of the identifier. Same as your
line.

To Rick: as people on GoT already suggested, it might be a bug in
SQLite.

FB
 
Back
Top