Reputation: 6799
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
sqlite_conn
then it says '... is a field but is used like a type'SQLiteConnection
then it says ... method must have a return type("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?
Upvotes: 1
Views: 5920
Reputation: 33491
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.
Upvotes: 0
Reputation: 63065
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();
}
}
Upvotes: 1
Reputation: 3095
You need to instantiate the SQLiteDataReader class by something like
SQLiteDataReader reader = sqlite_cmd.ExecuteReader();
See http://zetcode.com/db/sqlitecsharp/read/
Upvotes: 0
Reputation: 17590
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();
}
}
}
Upvotes: 0