English 中文(简体)
• 如何将C#与QQLite数据库连接起来
原标题:How to connect C# app with SQLite database

我想与一个SQ数据库连接。 请介绍一下《工作守则》。 我还希望将数据网格与数据库联系起来。 我使用这一守则,但并不工作:

private DataTable dt;
public Form1()
{
    InitializeComponent();
    this.dt = new DataTable();
}
private SQLiteConnection SQLiteConnection;
private void DataClass()
{
    SQLiteConnection = new SQLiteConnection("Data Source=PasswordManager.s3db;Version=3;");
}
private void GetData()
{
    SQLiteDataAdapter DataAdapter;
    try
    {
    SQLiteCommand cmd;
    SQLiteConnection.Open();  //Initiate connection to the db
    cmd = SQLiteConnection.CreateCommand();
    cmd.CommandText = "select*from PasswordManager;";  //set the passed query
    DataAdapter = new SQLiteDataAdapter(cmd);
    DataAdapter.Fill(dt); //fill the datasource
    dataGridView1.DataSource = dt;
}
catch(SQLiteException ex)
{
    //Add your exception code here.
}
SQLiteConnection.Close();
问题回答

您可使用。 系统.Data.SQLite ADO.NET提供商。 一旦你下载并提到大会,它就非常简单。 NET代码:

using (var conn = new SQLiteConnection(@"Data Source=test.db3"))
using (var cmd = conn.CreateCommand())
{
    conn.Open();
    cmd.CommandText = "SELECT id FROM foo";
    using (var reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            int id = reader.GetInt32(reader.GetOrdinal("id"));
            ...
        }
    }
}

除了Darin提供的答复外,在Kumite(从我记得的角度来看)没有“准确的数据库”。 当你启动“SQLiteConnection”时,如果某个数据库(db3)不存在,它就自动从那里创建......,那么你可以制作表格。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签