【问题标题】:sqlite database connection class in C#C#中的sqlite数据库连接类
【发布时间】:2013-07-18 08:31:21
【问题描述】:

我对 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 数据库连接类吗?

【问题讨论】:

标签: c# sqlite


【解决方案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();
     }   

}

【讨论】:

  • 感谢您的回复,我已经设法使用您的解决方案消除了错误,但是当我使用像这样的类时 Database db = new Database(); db.CommandText = "INSERT INTO test (id, text) VALUES (1, 'Test Text 1');"; 它给了我一个错误,在 @ 下划线987654324@。你能告诉我为什么会这样吗?
  • 你的数据库类中没有CommandText,你有sqlite_cmd。称它为db.sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (1, 'Test Text 1')";
  • 我刚试过db.sqlite_cmd.CommandText,但这次它给了我错误,下划线sqlite_cmd。谢谢
  • 错误信息是... is inaccessible due to its protection level
  • 您需要将database 课程和sqlite_conn,sqlite_cmd, sqlite_datareader 设为公开,请在答案中查看我的代码
【解决方案2】:

您不能在不同的行中初始化字段(方法之外)。把你的班级改成这样:

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

【讨论】:

    【解决方案3】:

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

    SQLiteDataReader reader = sqlite_cmd.ExecuteReader();
    

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

    【讨论】:

      【解决方案4】:

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

      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#。

      【讨论】:

      • 是的,你说得对,我应该先把C#的基础学好。否则我将无法找出错误原因
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多