簡體   English   中英

如何在現有數據庫(SQLite數據庫)上使用C#查詢?

[英]How do I use C# queries on a prexisting database(SQLite database)?

我遇到了一個問題,其中我的C#代碼正在創建一個全新的數據庫,而不是使用現有的數據庫。 然后我的程序遇到錯誤,即使程序中已有數據庫,該程序也找不到表來插入信息,因為代碼本身正在查看新表。 這是我的代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Finisar.SQLite;

namespace WestSlope
{
    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SQLiteConnection sqlite_conn;
        SQLiteCommand sqlite_cmd;
        //SQLiteDataAdapter sqlite_datareader;

        sqlite_conn = new SQLiteConnection("DataSource=ClientLogDB.db;Version=3;New=True;Compress=True;");

        //open conection
        sqlite_conn.Open();

        //create sql commands
        sqlite_cmd = sqlite_conn.CreateCommand();

        //Let SQLite command know query is known
        sqlite_cmd.CommandText = "INSERT INTO ASAM (ASAMone, ASAMtwo, ASAMthree, ASAMfour, ASAMLim, ASAMLimEX) VALUES ('Had to call', 'Reffered', 'Had to call', 'Watched', 1 , 'Injured legs');"
;

        //execute query
        sqlite_cmd.ExecuteNonQuery();











        sqlite_conn.Close();
    }
  }
}

該代碼應該執行的操作是,當用戶按下按鈕時,程序會將信息保存到預先存在的數據庫中。 但是,如您所見,該程序正在建立一個新數據庫,而不是使用現有數據庫。

在連接字符串中使用new=false可以使用現有的數據庫文件。 以下應該是連接字符串:

sqlite_conn = new SQLiteConnection("DataSource=ClientLogDB.db;Version=3;New=False;Compress=True;");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM