1

I am very new to C#, I am trying to create a sqlite database connection class. I have created a new class file by clicking on my project name> Add > Class. I have the following code in this file.

The problem is I am getting error in every lines after SQLiteDataReader

  1. If I hover over sqlite_conn then it says '... is a field but is used like a type'
  2. if I hover over SQLiteConnection then it says ... method must have a return type
  3. If I hover over ("Data Source=database.db;Version=3;New=True;Compress=True;") then it says Type expected

`

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Finisar.SQLite;

namespace learningCsharp
{
class database
{
    // We use these three SQLite objects:
    SQLiteConnection sqlite_conn;
    SQLiteCommand sqlite_cmd;
    SQLiteDataReader sqlite_datareader;

    // Getting error in every lines after this 

    // create a new database connection:
    sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");

   //open the connection:
     sqlite_conn.Open();

   // create a new SQL command:
     sqlite_cmd = sqlite_conn.CreateCommand();

}
}

Could you please help me to solve this problem and create a working sqlite database connection class?

black_belt
  • 6,601
  • 36
  • 121
  • 185

4 Answers4

1

You need to put the line in error in a class constructor or a method.

public class database
{
    // We use these three SQLite objects:
    public SQLiteConnection sqlite_conn;
    public SQLiteCommand sqlite_cmd;
    public SQLiteDataReader sqlite_datareader;

    public database()
    {
         sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");
         sqlite_conn.Open();
         sqlite_cmd = sqlite_conn.CreateCommand();
     }   

}
Damith
  • 62,401
  • 13
  • 102
  • 153
  • Thanks for your reply, I have managed to get rid of the errors using your solution, but when I am using the class like this `Database db = new Database();` `db.CommandText = "INSERT INTO test (id, text) VALUES (1, 'Test Text 1');";` it is giving me an error underlining the `CommandText`. Could you please tell me why is that happening? – black_belt Jul 19 '13 at 07:17
  • there is no `CommandText` in your database class, you have `sqlite_cmd`. call it like `db.sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (1, 'Test Text 1')";` – Damith Jul 19 '13 at 07:22
  • I just tried `db.sqlite_cmd.CommandText` but this time its giving me error underlining `sqlite_cmd` . Thanks – black_belt Jul 19 '13 at 07:25
  • The error message is ... `is inaccessible due to its protection level` – black_belt Jul 19 '13 at 07:29
  • you need to make your `database` class and the `sqlite_conn,sqlite_cmd, sqlite_datareader` as public, check my code in the answer – Damith Jul 19 '13 at 07:30
  • Thank you very much for your help, I have manged to get rid of the errors :) – black_belt Jul 19 '13 at 07:53
0

You can't initialize fields (outside of methods) in different lines. Change your class to this:

namespace learningCsharp
{
    class Database
    {
        // We use these three SQLite objects:
        SQLiteConnection sqlite_conn;
        SQLiteCommand sqlite_cmd;
        SQLiteDataReader sqlite_datareader;

        //constructor called when initializing new instance
        public Database()
        {
            sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");
            sqlite_conn.Open();
            sqlite_cmd = sqlite_conn.CreateCommand();
        }
    }
}
gzaxx
  • 17,312
  • 2
  • 36
  • 54
0

You need to instantiate the SQLiteDataReader class by something like

SQLiteDataReader reader = sqlite_cmd.ExecuteReader();

See http://zetcode.com/db/sqlitecsharp/read/

andrewb
  • 2,995
  • 7
  • 54
  • 95
0

You never create methods or constructors.

class database
{
    // Here you define properties: OK
    SQLiteConnection sqlite_conn;
    SQLiteCommand sqlite_cmd;
    SQLiteDataReader sqlite_datareader;


    // Then, you do stuff to them: NOT OK
    sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");

   //open the connection:
     sqlite_conn.Open();

   // create a new SQL command:
     sqlite_cmd = sqlite_conn.CreateCommand();

}
}

You could fix it, by putting your "doing-stuff" code in a method:

class database
{
    // Here you define properties: OK
    SQLiteConnection sqlite_conn;
    SQLiteCommand sqlite_cmd;
    SQLiteDataReader sqlite_datareader;

    void public DoStuff() {
       // Then, you do stuff to them: NOT OK
       sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");

      //open the connection:
      sqlite_conn.Open();

      // create a new SQL command:
      sqlite_cmd = sqlite_conn.CreateCommand();

    }
  }
}

Then, you can instantiate and run like this:

database db = new database();
db.DoStuff();

This is all basic C#, OO programming. I strongly advise you learn C# first, before getting into SQLite database programming.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • Yes, You're right, I should learn the basics of C# well first. Otherwise I won't be able to find out the causes of errors – black_belt Jul 19 '13 at 07:21