C# SQLite使用举例查询数据库所有表所有列,插入表和插入的方法

本文详细介绍了一个使用C#和SQLite实现的桌面应用程序,该程序提供了数据库管理功能,包括数据库和表的创建、列的添加、数据的插入,以及通过界面进行数据库、表和列的选择与展示。

主窗体类

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 System.Data.SQLite;
using System.IO;

namespace SqlitTest
{
   
   
    public partial class Form1 : Form
    {
   
   
        static  string DbName = "testc";
        static string CurTable = "表2";
        static string CurColNum = "列1";
        static string filePath = "db";
        public Form1()
        {
   
   
            InitializeComponent();
            UpdateDbname();
        }
        private void UpdateDbname()
        {
   
   
            string[] files = Directory.GetFiles(filePath, "*.db");
            foreach (string i in files)
            {
   
   
                string str = i.Replace($"{filePath}\\","");
                str = str.Replace(".db", "");
               if( !comboBoxDbNameList.Items.Contains(str))
                comboBoxDbNameList.Items.Add(str);
            }
            if (comboBoxDbNameList.Items.Contains(DbName))
            {
   
   
                int m = 0;
                foreach (string j in comboBoxDbNameList.Items)
                {
   
   
                    if (DbName == j)
                    {
   
   
                      //  comboBoxDbNameList.SelectedIndex = m;
                    }
                    m++;
                }
            }
            UpdateTbName2();
        }
        //显示表信息的第一中方法
        private void UpdateTbName()
        {
   
   
            DataTable dt = new DataTable();
            using (SQLiteConnection connection = new SQLiteConnection($"Data Source=db/{DbName}.db"))
            {
   
   
                if (connection.State != ConnectionState.Open)
                    connection.Open();
                using (SQLiteCommand command = new SQLiteCommand(connection))
                {
   
   
                    command.CommandText = "select * from sqlite_master where type = 'table' order by name; ";                 
                    using (SQLiteDataAdapter sda = new SQLiteDataAdapter(command))
                    {
   
   

                        sda.Fill(dt);//将数据库中表信息显示
                    }
                    comboBoxTbNameList.Items.Clear();
                    if (dt.Rows.Count > 0)
                    {
   
   
                        DataRow[] tbs = dt.Select("type = 'table'");
                        foreach (var tb in tbs)
                        comboBoxTbNameList.Items.Add(
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值