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?

2

4 Answers 4

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();
     }   

}
Sign up to request clarification or add additional context in comments.

6 Comments

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?
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')";
I just tried db.sqlite_cmd.CommandText but this time its giving me error underlining sqlite_cmd . Thanks
The error message is ... is inaccessible due to its protection level
you need to make your database class and the sqlite_conn,sqlite_cmd, sqlite_datareader as public, check my code in the answer
|
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();
        }
    }
}

Comments

0

You need to instantiate the SQLiteDataReader class by something like

SQLiteDataReader reader = sqlite_cmd.ExecuteReader();

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

Comments

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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.