1

我对 C# 很陌生,我正在尝试创建一个 sqlite 数据库连接类。我通过单击我的项目名称 > 添加 > 类创建了一个新的类文件。我在这个文件中有以下代码。

问题是我在之后的每一行都出现错误SQLiteDataReader

  1. 如果我将鼠标悬停在上面,sqlite_conn那么它会说'......是一个字段,但用作类型'
  2. 如果我将鼠标悬停在SQLiteConnection上面,它会说...方法必须有一个返回类型
  3. 如果我将鼠标悬停在上面,("Data Source=database.db;Version=3;New=True;Compress=True;")那么它会说 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();

}
}

你能帮我解决这个问题并创建一个有效的sqlite数据库连接类吗?

4

4 回答 4

1

您需要在类构造函数或方法中输入错误的行。

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

}
于 2013-07-19T06:45:47.070 回答
0

您不能在不同的行中初始化字段(方法之外)。将您的课程更改为:

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();
        }
    }
}
于 2013-07-19T06:49:18.270 回答
0

您需要通过类似的方式实例化 SQLiteDataReader 类

SQLiteDataReader reader = sqlite_cmd.ExecuteReader();

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

于 2013-07-19T06:50:26.713 回答
0

你永远不会创建方法或构造函数。

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

}
}

您可以通过将“做事”代码放入方法中来修复它:

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

    }
  }
}

然后,您可以像这样实例化并运行:

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

这都是基本的 C#、OO 编程。我强烈建议你在进入 SQLite 数据库编程之前先学习 C#。

于 2013-07-19T06:55:21.410 回答