2016-11-12 115 views
0

我已经创建为WPF应用程序设置文件,数据库是SQLite数据库。安装完成后,SQLite数据库位于C文件夹中的此路径,C# - 访问WPF应用程序的SQLite数据库

C:\ Program Files(x86)\ myCompany \ myScanApp。

enter image description here

然后我写的连接字符串这样的方式建立对于SQLite数据库的连接,

static SQLiteConnection dbConnection = new SQLiteConnection(@"Data Source=C:\Program Files (x86)\myCompany\myScanApp\test.s3db;"); 

然后,我创建了一个安装文件再次,然后运行另一台PC上这个应用程序。但是,它无法在个人电脑上运行。我如何在应用程序中访问数据库?

回答

1
using(SQLiteConnection conn= new SQLiteConnection(@"Data Source=C:\Program Files (x86)\myCompany\myScanApp\test.s3db;")) 
{ 
    conn.Open(); 

    SQLiteCommand command = new SQLiteCommand("Select * from yourTable", conn); 
    SQLiteDataReader reader = command.ExecuteReader(); 

    while (reader.Read()) 
     Console.WriteLine(reader["YourColumn"]); 


    reader.Close(); 
} 

这样的事情应该可以工作。这里整个文章Getting started with SQLite in C#

+0

嗨我也写了这个连接sting,但它导致错误,当我试图安装后运行应用程序。我怎么能够 ? – lashja

+0

@AbhilashJA引起错误对我来说没有任何意义。 https://www.connectionstrings.com/sqlite/这里有关连接字符串的文章。不要使用静态连接字符串!整个代码是方法的一部分。 – mybirthname

+0

是的。对...它正在工作。我写了这样的连接字符串: - public SQLiteConnection dbConnection = new SQLiteConnection(@“Data Source = C:\ Program Files(x86)\ hedronix \ qScan \ test.s3db;”); 谢谢。 – lashja