winfrom程序操作sqlite数据库demo源码
时间: 2023-07-10 09:02:19 浏览: 162
### 回答1:
以下是一个使用 WinForms 程序操作 SQLite 数据库的示例源码:
```csharp
using System;
using System.Data.SQLite;
using System.Windows.Forms;
namespace SQLiteDemo
{
public partial class MainForm : Form
{
private SQLiteConnection connection;
private SQLiteCommand command;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
// 连接到 SQLite 数据库
connection = new SQLiteConnection("Data Source=database.db;Version=3;");
connection.Open();
command = new SQLiteCommand(connection);
// 创建表格
command.CommandText = "CREATE TABLE IF NOT EXISTS Person (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Age INTEGER)";
command.ExecuteNonQuery();
}
private void btnAdd_Click(object sender, EventArgs e)
{
// 插入数据
string name = tbName.Text;
int age = Convert.ToInt32(tbAge.Text);
command.CommandText = "INSERT INTO Person (Name, Age) VALUES (@name, @age)";
command.Parameters.AddWithValue("@name", name);
command.Parameters.AddWithValue("@age", age);
command.ExecuteNonQuery();
MessageBox.Show("数据已添加成功!");
}
private void btnDisplay_Click(object sender, EventArgs e)
{
// 显示数据
command.CommandText = "SELECT * FROM Person";
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
int id = Convert.ToInt32(reader["Id"]);
string name = reader["Name"].ToString();
int age = Convert.ToInt32(reader["Age"]);
MessageBox.Show($"ID: {id}\nName: {name}\nAge: {age}");
}
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
// 关闭数据库连接
command.Dispose();
connection.Close();
}
}
}
```
这个示例中,我们创建了一个名为 `MainForm` 的窗体,包含了用于输入名称和年龄的文本框,以及用于添加和显示数据的按钮。在窗体加载时,我们首先连接到 SQLite 数据库并创建了一个名为 `Person` 的表格。通过点击“添加”按钮,我们可以将输入的名称和年龄插入到数据库中。点击“显示”按钮,我们会从数据库中查询所有的数据,并在弹出消息框中显示出来。
### 回答2:
下面是一个使用Winform程序操作SQLite数据库的示例代码:
```csharp
using System;
using System.Data.SQLite;
using System.Windows.Forms;
namespace WinformSQLiteDemo
{
public partial class MainForm : Form
{
private SQLiteConnection _conn;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
// 创建数据库连接
_conn = new SQLiteConnection("Data Source=MyDatabase.db;Version=3;");
_conn.Open();
// 创建表
string createTableQuery = "CREATE TABLE IF NOT EXISTS Users (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Age INTEGER)";
SQLiteCommand createTableCmd = new SQLiteCommand(createTableQuery, _conn);
createTableCmd.ExecuteNonQuery();
// 查询并显示数据
string selectQuery = "SELECT * FROM Users";
SQLiteCommand selectCmd = new SQLiteCommand(selectQuery, _conn);
SQLiteDataReader reader = selectCmd.ExecuteReader();
while (reader.Read())
{
string name = reader.GetString(1);
int age = reader.GetInt32(2);
dataGridView.Rows.Add(name, age);
}
// 关闭连接
reader.Close();
_conn.Close();
}
private void addButton_Click(object sender, EventArgs e)
{
string name = nameTextBox.Text;
int age = Convert.ToInt32(ageTextBox.Text);
// 插入数据
_conn.Open();
string insertQuery = "INSERT INTO Users (Name, Age) VALUES (@name, @age)";
SQLiteCommand insertCmd = new SQLiteCommand(insertQuery, _conn);
insertCmd.Parameters.AddWithValue("@name", name);
insertCmd.Parameters.AddWithValue("@age", age);
insertCmd.ExecuteNonQuery();
// 更新DataGridView
dataGridView.Rows.Add(name, age);
// 清除输入框
nameTextBox.Text = "";
ageTextBox.Text = "";
_conn.Close();
}
}
}
```
这个示例代码包含一个主窗体(MainForm),它在加载时创建了一个SQLite数据库连接,并创建了一个名为"Users"的表。在加载时,它还从数据库中读取已有的用户数据,并将其显示在一个DataGridView控件中。
在窗体中还有一个添加按钮(addButton),当用户输入姓名和年龄后点击此按钮,程序将把数据插入到数据库中,并更新DataGridView以显示新添加的数据。
请注意,该示例中的数据库文件名为"MyDatabase.db",你可以根据需要修改它。
### 回答3:
以下是一个用于操作SQLite数据库的WinForm程序的示例源代码:
```csharp
using System;
using System.Data;
using System.Data.SQLite;
using System.Windows.Forms;
namespace SQLiteDemo
{
public partial class MainForm : Form
{
private SQLiteConnection connection;
private SQLiteCommand command;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
string connectionString = "Data Source=database.db;Version=3;";
connection = new SQLiteConnection(connectionString);
command = new SQLiteCommand(connection);
connection.Open();
CreateSampleTable();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
connection.Close();
connection.Dispose();
}
private void CreateSampleTable()
{
string createTableQuery = "CREATE TABLE IF NOT EXISTS Users (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Age INTEGER)";
command.CommandText = createTableQuery;
command.ExecuteNonQuery();
}
private void btnInsert_Click(object sender, EventArgs e)
{
string name = txtName.Text;
int age = Convert.ToInt32(txtAge.Text);
string insertQuery = $"INSERT INTO Users (Name, Age) VALUES ('{name}', {age})";
command.CommandText = insertQuery;
command.ExecuteNonQuery();
MessageBox.Show("数据插入成功!");
LoadData();
}
private void btnDelete_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(txtId.Text);
string deleteQuery = $"DELETE FROM Users WHERE Id = {id}";
command.CommandText = deleteQuery;
command.ExecuteNonQuery();
MessageBox.Show("数据删除成功!");
LoadData();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(txtId.Text);
string name = txtName.Text;
int age = Convert.ToInt32(txtAge.Text);
string updateQuery = $"UPDATE Users SET Name = '{name}', Age = {age} WHERE Id = {id}";
command.CommandText = updateQuery;
command.ExecuteNonQuery();
MessageBox.Show("数据更新成功!");
LoadData();
}
private void LoadData()
{
string selectQuery = "SELECT * FROM Users";
SQLiteDataAdapter adapter = new SQLiteDataAdapter(selectQuery, connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
dataGridView.DataSource = dataTable;
}
}
}
```
这个示例程序创建了一个包含三个字段(Id、Name和Age)的Users表。通过使用不同的按钮,可以插入、删除和更新数据库中的数据。每次操作完成后,数据表将被重新加载到DataGridView控件中以显示更新后的内容。请确保已在程序所在的目录下创建了一个名为database.db的SQLite数据库文件。
阅读全文