使用C#通过其他方法打开内存SQLite连接

时间:2018-07-24 20:13:31

标签: c# sqlite

我正在使用C#和SQLite并努力创建内存数据库,并使用一种方法添加两个空白表,并使用第二种方法(在同一类中)将数据添加到表中。

当我运行下面的代码时,我得到了错误:

"SQL logic error no such table: node_table"

有人知道为什么会发生此错误吗?

下面提供的方法(在一些没有运气的情况下我尝试过其他事情的评论)

    public static void NewDatabase()
    {

        Debug.WriteLine("NewDatabase has run");
        //
        //using "file:memdb1?mode=memory&cache=shared" triggered the error:
        //Data Source cannot be empty.  Use :memory: to open an in-memory database
        var m_dbConnection = new SQLiteConnection("Data Source=:memory:");
        m_dbConnection.Open();

        string sql;

        sql = "CREATE TABLE node_table(id INTEGER PRIMARY KEY, node_name TEXT, x REAL, y REAL, z_cover REAL)";
        SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
        command.ExecuteNonQuery();

        sql = "CREATE TABLE conduit_table(id INTEGER PRIMARY KEY, conduit_name TEXT, usmh_id REAL, dsmh_id REAL, length REAL)";
        command = new SQLiteCommand(sql, m_dbConnection);
        command.ExecuteNonQuery();

        m_dbConnection.Close(); //Tried toggling this on and off


    }

    public static void InsertNode(string node_name, float x, float y, float z_cover)
    {
        var m_dbConnection = new SQLiteConnection("Data Source=:memory:");
        m_dbConnection.Open();
        string sql = "INSERT INTO node_table(node_name, x, y, z_cover) VALUES (@node_name, @x, @y, @z_cover)";
        SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
        command.Parameters.AddWithValue("@node_name", node_name);
        command.Parameters.AddWithValue("@x", x);
        command.Parameters.AddWithValue("@y", y);
        command.Parameters.AddWithValue("@z_cover", z_cover);
        command.ExecuteNonQuery();
        //Error reads: SQL logic error no such table: node_table
    }

编辑:

在主入口点调用NewDatabase,如下所示:

namespace MainForm
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Parent());
            SqlConnections.NewDatabase();

        }
    }
}

在Winform中的click事件上调用InsertNode

    private void Plan_Load(object sender, EventArgs e) //need to change the name of this function
    {
        //Thic code gets the cursor position relative to the screen
        //this.Cursor = new Cursor(Cursor.Current.Handle);

        if (Globals.addNode == true)
        {
            var relativePoint = this.PointToClient(Cursor.Position);

            float x = relativePoint.X;
            float y = relativePoint.Y;
            SqlConnections.InsertNode("1", x, y, 2);
        }
    }

编辑:SQLite的安装版本如下所示。 文档尚不可用,因此一直依赖于主要的SQLite文档。

SQL Version Information

1 个答案:

答案 0 :(得分:2)

当您关闭最后一个连接时,内存数据库将消失。

保持连接打开并在功能之间传递。

或者,保持至少一个打开的连接,或者保持另一个连接。

相关问题