sqlite数据库自动保存全部控件textbox,numericalupdown,checkebox的内容,包括容器panel,groupbox,tabcontrol内的控件
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace sql
{
class sqliteautosavecontrols
{
public static void show(Control.ControlCollection ct)
{
connectToDatabase();
//createTable();
SQLiteDataReader reader = printHighscores();
while (reader.Read())
{
foreach (Control item in ct)
{
if (item is GroupBox)
{
show(item.Controls);
}
if (item is Panel)
{
show(item.Controls);
}
if (item is TabControl)
{
show(item.Controls);
}
if (item is TextBox)
{
if (reader["name"].ToString() == item.Name)
{
item.Text = reader["score"].ToString();
}
}
if (item is Label)
{
if (reader["name"].ToString() == item.Name)
{
item.Text = reader["score"].ToString();
}
}
if (item is CheckBox)
{
if (reader["name"].ToString() == item.Name)
{
CheckBox tx = item as CheckBox;
tx.Checked = Convert.ToBoolean(reader["score"]);
}
}
if (item is NumericUpDown)
{
if (reader["name"].ToString() == item.Name)
{
NumericUpDown tx = item as NumericUpDown;
tx.Value = Convert.ToDecimal(reader["score"]);
}
}
}
}
}
static void onlyonesavesqliteforcontrols(Control.ControlCollection ct)
{
delecttable();
createTable();
save( ct);
}
public static void save(Control.ControlCollection ct)
{
foreach (Control item in ct)
{
if (item is GroupBox)
{
save(item.Controls);
}
if (item is Panel)
{
save(item.Controls);
}
if (item is TabControl)
{
save(item.Controls);
}
if (item is CheckBox)
{
CheckBox c = item as CheckBox;
fillTable(c.Name, c.Checked.ToString());
}
if (item is TextBox)
{
fillTable(item.Name, item.Text);
}
if (item is Label)
{
fillTable(item.Name, item.Text);
}
if (item is NumericUpDown)
{
NumericUpDown c = item as NumericUpDown;
fillTable(c.Name, c.Value.ToString());
}
}
}
//数据库连接
public static SQLiteConnection m_dbConnection;
//创建一个空的数据库
public static void createNewDatabase()
{
if (!Directory.Exists("database\\"))//如果不存在就创建 dir 文件夹
Directory.CreateDirectory("database\\");
SQLiteConnection.CreateFile("database\\MyDatabase.sqlite");
}
//创建一个连接到指定数据库
public static void connectToDatabase()
{
if (!System.IO.File.Exists("database\\MyDatabase.sqlite"))
{
createNewDatabase();
}
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
}
//在指定数据库中创建一个table
public static void createTable()
{
string sql = "create table highscores (name varchar(20), score varchar(20))";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
//插入一些数据
public static void fillTable(string s1, string s2)
{
string sql = "insert into highscores (name, score) values ('" + s1 + "', '" + s2 + "')";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
public static void delecttable()
{
string sql = "DROP TABLE highscores";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
//使用sql查询语句,并显示结果
public static SQLiteDataReader printHighscores()
{
string sql = "select * from highscores";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
return command.ExecuteReader();
}
}
}
本文介绍了一种使用SQLite数据库自动保存Windows Forms应用中各种控件状态的方法,包括TextBox、NumericUpDown、CheckBox等,覆盖了Panel、GroupBox、TabControl内的所有控件。通过创建数据库、表格,实现控件状态的读取和保存。
260

被折叠的 条评论
为什么被折叠?



